.NET Type System, Part II - C# Classes
(Page 2 of 7 )
A class in C# is much like any other class in other Object Oriented programming languages, but it's encapsulated in one file (unlike C++) with the extension .cs. Although you can write as many classes as you want in that one file, it's not a good programming practice.
A class contains members like constructors, destructors, nested types, events, delegates, constants, indexers, properties, methods and fields. These are the members of a class, so you can define one or more of them. The expression class member refers to a piece of data or an operation that exists in the class, so methods, constructors, destructors and properties represent operations of a given class which we can call functions of the class, too.
In C# all classes are reference types, so they need the new operator in order to be allocated on the Managed Heap. You will learn more about classes and class members in the following article.
C# Delegates
If you are a C++ programmer then you can think of C# delegates as function pointers (but in .NET it's type safe). A delegate is a pointer to a method in a class. We define a delegate class to declare the signature of the methods for this delegate. Windows Forms components use delegates extensively to provide the functionality of events. In order to grasp the concepts of delegates and events, you will need to look at the Generated MSIL code for these types, and this is exactly what we will do later. There is a complete, detailed article about delegates and another one about events coming later in this series, so I won't go into details here.
C# Arrays
C# arrays have syntax similar to Java arrays and C++ arrays, but they work differently because C# arrays have built-in methods and properties for manipulating them, which Java and C++ lack. Arrays have a base class, which is System.Array, and we will look at some of the provided methods later on the article. You can create an array of value types and reference types. We said before that Value-Types allocated on the Stack; it's true, but it needs a little explanation, since we are covering reference types in this section.
Arrays are reference types, so when you declare a new array that contains value types, the CLR will allocate memory space for the array elements on the Managed Heap (despite their being value types). When you begin to use the elements of the array, they will be copied to the stack. So, to put it simply, value types are declared when they're used, as we will see in an example about this later on.
Next: Using Arrays >>
More .NET Articles
More By Michael Youssef