Home > 2010 Winter BTI420 > Learning C# – the top ten list

Learning C# – the top ten list

January 18, 2010 Leave a comment Go to comments

When we begin to learn a new computer programming language, we often try to get acquainted with the language by applying our current knowledge to its characteristics. This “top ten” list captures this process.

As a second-year Software Development student, you have some experience with two or three programming languages. Let’s begin to learn about C# by asking questions in the form of a “top ten” list. (Note that you can ask these questions about ANY language.)

The “top ten” list of questions

Here is the “top ten” list of questions to ask when learning C#:

1. How do I create/edit/save the source code? What editor do I use? Where do I store the source code? What is the source code file naming convention?

2. How do I build/compile and then execute/run my program? Is there a compiler? Is there a host execution (aka “runtime”) environment?

3. What is the program’s entry point? Syntactically, what coding convention must I follow?

4. What data types can I use? Are they categorized (e.g. value/stack, reference/heap), and if so, what do I need to know about their characteristics (e.g. size, initial value, precision, convertibility, etc.)?

5. How do I declare new variables/fields of a data type? How do I instantiate them? How do I refer to and use these variables/fields? (Note that a type could be a class declaration for an object-oriented language and platform.)

6. What operators are available to me in this language? (How do they compare to other languages that I’m familiar with?)

7. What is the syntax of a statement, and of an expression? What delimiters are used? How is scope and hierarchy expressed?

8. What syntax enables me to process a decision (e.g. if-else, or switch-case)?

9. What syntax enables me to process repeatedly (e.g. for, while)?

10. How are functions (methods) declared? What rules are there for argument/parameter declaration? How are functions used/called? Are the arguments passed by value, or by reference?

The answers

Here are the answers. We cover them in class, and we go through some examples.

1. How do I create/edit/save the source code? What editor do I use? Where do I store the source code? What is the source code file naming convention?

For C# ASP.NET MVC web applications…

  • Visual Studio is used
  • You work on the local file system, and deploy to a server
  • A “model” source code file describes your app’s data model
  • A “controller” source code file contains app logic
  • A “view” source code file contains markup and code expressions for the user interface

2. How do I build/compile and then execute/run my program? Is there a compiler? Is there a host execution (aka “runtime”) environment? Does it control the application lifecycle?

In Visual Studio, you will often “run without debugging”, Ctrl+F5. Your work opens in a browser.

The web server uses an ASP.NET execution runtime environment to create an instance of your controller class, and deliver its results to the browser user. The runtime is in control. Your code is essentially a collection of event handlers.

3. What is the program’s entry point? Syntactically, what coding convention must I follow?

The ASP.NET runtime has an entry point. Your code’s entry point is document-based. When a user requests a document – your “web form” page – the runtime creates an instance of your page, and delivers its results to the browser user.

There is a well-defined page lifecycle, and page logic processing sequence. We’ll learn more about that soon. For now, you can count on the following:

  1. The Page_Load method runs first
  2. Then, the method for the event that caused the page to be reloaded (known as a postback) runs

4. What data types can I use? Are they categorized (e.g. value/stack, reference/heap), and if so, what do I need to know about their characteristics (e.g. size, initial value, precision, convertibility, etc.)?

The .NET Framework offers its languages a “common type system”. They include a selection of “value” types (where the type’s data is a fixed size, and lives on a stack), and “reference” types (where the type’s data varies in size, and the stack content is a pointer to the type’s data in “heap” memory).

All .NET Framework types are objects, even types you think of as scalars in other languages. It is a rich type system, and we’ll learn about and use the types as we go along.

We also create our own types (classes).

5. How do I declare new variables/fields of a data type? How do I instantiate them? How do I refer to and use these variables/fields? (Note that a type could be a class declaration for an object-oriented language and platform.)

Examples…

Declaration syntax:

typeName variableName;

Instantiation syntax:

typeName variableName = new typeName(constructor, arguments, go, here);

Referring to and using syntax:

variableName = someValue;

Response.Write(variableName.ToString());

6. What operators are available to me in this language? (How do they compare to other languages that I’m familiar with?)

A rich set of operators are available, using a comfortable syntax.

7. What is the syntax of a statement, and of an expression? What delimiters are used? How is scope and hierarchy expressed?

C# is a C-family language, so its statement syntax is similar.

Expressions are also similar, and you can use operators on objects without conversions.

Statements end with a ; semicolon, just like C.

Curly braces { } are used as code block delimiters. Scoping follows rules similar to C. For type and member (e.g. methods, properties) visibility, there is a default, but there are scoping keywords that can be used to modify the scope.

8. What syntax enables me to process a decision (e.g. if-else, or switch-case)?

C# is a C-family language, so its statement syntax is similar. if-else (including the ? : syntax) and switch-case are available.

9. What syntax enables me to process repeatedly (e.g. for, while)?

C# is a C-family language, so its statement syntax is similar. for and while are available. There is also a foreach enumerator form of the for statement.

10. How are functions (methods) declared? What rules are there for argument/parameter declaration? How are functions used/called? Are the arguments passed by value, or by reference?

In a class, methods are declared like this:

scope returnTypeName methodName(typeName argument1, typeName argument2) { }

Arguments must include a type name declaration. There are options for parameters for directionality, defaults, and by-reference (mentioned next).

In C#, pass-by-value is the default. The method receives a copy of the value from the caller.


Advertisements
Categories: 2010 Winter BTI420
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: