Text Size

REFERENCE DATA TYPES

PDF 

Tutorials

(0 votes, average: 0 out of 5)

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.

 

Trackback(0)
Comments (0)add comment

Write comment

busy

Site Statistics

Stats
Total Members
: 208
Total Discussion
: 0
Total Albums
: 0
Total Photos
: 0
Total Bulletins
: 0
Total Activities
: 1
Total Wall Posts
: 1