Tutorials
Desktop Programming
C Sharp
REFERENCE DATA TYPES
Written by Christopher Thursday, 29 May 2008 13:43
Tutorials
INTRODUCTION
Reference types provide excellent flexibility and performance of large structures. This is the reason that classes are reference types and structs are data value types. As you will probably know, reference types do not store the actual value of the object but a pointer to a memory location. This pointer is stored on the stack whereas the object itself is located in the managed heap.
USING REFERENCE DATA TYPES
There are about 2500 built-in reference types. Everything not derived from System.ValueType is a reference type. The most common of these types are:
1. object
2. string
3. array of data
4. streams
The object type is the one from which all other intrinsic and user-defined types derive. Both classes and structs also derive from this object. You can bind an object to every type available in C#, such as an integer or a character. You can also box a value type object to an object in order to move it from the stack to the managed heap. Finally, the object type implements some basic methods that all other objects inherit. These functions include Equals(), GetHashCode(), and ToString().
A string object is also allocated on the heap. When you assign one string variable to another only the reference is copied. Both variables point to the same object. Bear in mind however, that when assigning a new value to a string the string’s value does not change. Instead, a new string is created, and the new value is assigned to it. Run the following code and try to explain what happens:
static void Main()
{
string S1;
string S2;
S1 = "Hello World";
S2 = S1;
Console.WriteLine(S1);
Console.WriteLine(S2);
S1 = "GoodBye World";
Console.WriteLine(S1);
Console.WriteLine(S2);
Console.ReadLine();
}
When assigning S2 to S1 they both refer to the same object. However, when changing the value of S1 a new string object is created. S2 and S1 do not point at the same object anymore.
String literals are enclosed in double quotes (“) as you will have noticed. C# also offers escape sequences, such as new line, alert, backspace etc... All escape sequences start with a backslash (\). If you need to include a directory in a string you should represent it with a double backslash (\\):
string S1 = "C:\\Home";
Alternatively, you could use the at (@) character that ignores escape sequences:
string S1 = @"C:\Home";
Arrays are declared using square brackets in the declaration part:
int[] myArray = { 1, 3, 3, 5 };
Arrays provide various methods for dealing with their internal data. The following code declares an array with some initial data and then sorts the array:
int[] myArray = { 13, 3, 8, 5 };
Console.WriteLine("{0},{1},{2},{3}", myArray[0], myArray[1], myArray[2], myArray[3]);
Array.Sort(myArray);
Console.WriteLine("{0},{1},{2},{3}", myArray[0], myArray[1], myArray[2], myArray[3]);
Finally, streams are being used for reading and writing to the disk or network. Various streams include memory streams, file streams, encryption streams and others.
DESTROYING REFERENCE TYPE OBJECTS
When a reference type is no longer referenced it should be destroyed or finalized. This is accomplished by the Garbage Collector. Garbage Collection periodically recovers memory as needed by disposing the items that are no longer referenced. Garbage collection can be triggered either automatically or by a call to GC.Collect function. Automatic garbage collection is optimized where all instances of objects are short-lived except for those declared on the beginning of the program.

| < Prev | Next > |
|---|
| Training Tip #1 :: Push-ups on the Swiss Ball admin 28.4.2009 7:07 |
Re:hello admin 21.4.2008 14:11 |
| Dell Inspiron 1545 Windows XP Drivers admin 12.4.2009 8:03 |
hello yuppy 21.4.2008 10:28 |
| Re:Get rich ebooks - are they really working? lann 23.4.2008 17:04 |
Re:Get rich ebooks - are they really working? admin 19.4.2008 14:08 |