C# Fundamentals - tutorial 1

Designed and Written by Michael Bartlett


In this lesson, I will provide you with an important overview of the way C# handles data. You'll also get to see the basic layout of a C# program in the annotated code listing that accompanies this tutorial. Please print this listing out using the accompanying source files and your Antechinus C# editor.

WHERE IT ALL BEGINS: SYSTEM.OBJECT

The most important thing to bear in mind about C# is that everything can be treated as an object. Those of you familiar with Object Oriented Programming (OOP) will recall that when an instance of a class is created (i.e. an object) in an OOP language, it may have a set of methods available to the programmer, depending on whether the author of the class deemed it necessary. In C#, all classes are inherited from the class, system.object. The programmers who created this class have deemed it necessary for the system.object class to have four methods, which all classes therefore inherit. One of these methods is called ToString(), and simply returns a class name or a value depending on what it is being applied to; you'll see it in action soon.

THE TWO DATA TYPES

C# theory segregates data types into two distinct groups: Value-types and Reference-types. A value type can be thought of as being something that directly contains its data, whereas a reference data type is one where the data is accessed indirectly through a reference variable which points to the data. Whenever a programmer creates an object of a reference-type, they must declare two things: the reference variable that refers (points) to the object, and the actual object itself; it is at this time that the programmer makes use of the new operator, which we'll come to see a bit later. If, however, you are only declaring the reference variable, but not pointing it at anything, then you can proceed in the manner shown below, where an_object is declared but doesn't point to anything. New should only be used when a reference is declared and will then immediately point to something.

Value and reference types (taken from listing 1x1.cs)

C# provides programmers with two predefined reference-types: object and string; all other reference types tend to be classes that either you or other C# programmers have created, which inherit from system.object automatically. You might be confused by the way the reference-data a_string of type string was created in the code-snippet; for some reason, unknown to myself, Microsoft have decided that even though all reference-types must be declared using the new operator, strings must be created in the value-type style. Any attempt made by the programmer to declare a string using the new operator will result in a compiler warning.

The predefined value-types are: sbyte, short, int, long, byte, ushort, uint, ulong, float, double, bool, char and decimal; again, these inherit directly from system.object. In addition to the value-types declared above, a programmer can also use enums and structs.

In light of the fact that you now understand both value-types and reference-types inherit from system.object, consider the folowing code:

An apparently nutty statement?

You might be wondering how C# applied the function ToString() to the value '6', since it hadn't been assigned to a data-type and therefore didn't inherit the system.object class's methods. This is achieved through a technique called 'Boxing'. Boxing occurs in C# when the compiler comes across a value or a value-type but needs to treat it as an object. Even though value-types inherit from system.object, they still need to be boxed in order to be recognised as objects. What takes place at this point simply involves the compiler boxing the value into a reference-type of type object (similar to Visual Basic's variant type), performing its calculation, and then casting the object back to its original simple value-type (known as 'Unboxing'). C# does all of this without us knowing, but if we were to do it ourselves, it would look like this:

Manual Boxing and Unboxing

Although we haven't dived in and written any 'real' code yet, you are now fully grounded in the basics of C# data types; this will stand you in good stead for the tutorials ahead. Before we move to tutorial 2, please familiarise yourself with Listing 1x1, which will also demonstrate the basic layout of a C# program. To compile the source file, either use an editor of your choice that can directly invoke the MS C# Compiler, or type csc at the command line, followed by the name of the file together with its extension; in this case it is: csc 1x1.cs.

There will be a lot of compiler warnings, due to the fact we have initialised a lot of variables in this program and not used them. This, however, is fine, since only compiler errors stop the program from compiling properly. The C# executable will have the same name as the source file and will be located alongside it. When running the program, the following output is obtained:

>1x1

SUMMARY


[OVERVIEW] [TUTORIAL 1] [TUTORIAL 2] [TUTORIAL 3] [TUTORIAL 4]