News:

Choose a design and let our professionals help you build a successful website   - ITAcumens

Main Menu

Reference Types in C#

Started by sukishan, Jul 11, 2009, 02:34 PM

Previous topic - Next topic

sukishan

Reference Types in C#:   

In the above example, assume that First we have two value type i and j. Also assume that the second variable is initialized with the value of the first one. It creates new copy in memory and after it the values of these variables will be next:

i = 10;
j = i;

   There are a few more things written in the above example for explaining the Reference Types in C#. At first, the variable ic1 of IntClass is created using dynamic memory allocation. Then we initialize the variable ic2 with value of ic1. This makes both the variables ic1 and ic2 referring to the same address. If we change a value of ic2, it automatically changes the value of ic1.

   Now, over to the discussions about the important value types used in C#. The category simple types contains some predefined or system types that also are commonly used in other programming languages. It contains integer types: byte, Sbyte, Long, Ulong, Short, Ushort, int, Uint. These common types differs only range of values and sign.

   Next simple type is character type. To declare a variable of this type need use keyword char. It can take values of characters or numbers as 16-digit symbol of the type Unicode.

   The Boolean type has only two values: true, false. But these values cannot be assigned with a 0 or 1 as in C++ language.

   Next category of simple types is floating point types. It contains two types float and double. Float type can get values in range from 1.5*10-45 to 3.4*1038. Double type has range of values from 5.0*10-324 to 1.7*10308.

   A structural value types are struct and enum. Struct is a the same as class but it uses real values not references. The following code snippet contains the definition for struct:

struct Point3D
{

public float m_x;
public float m_y;
public float m_z;

public float [] GetArray()
{

float [] arr = new float[3];
arr[0] = m_x;
arr[1] = m_y;
arr[2] = m_z;
return arr;

}

}


   The above is declaration for a simple structure of real 3D point. As you see a class declaration looks very similar to the struct except that the class also has a constructor.


Enumerated types can be used to create some set of identifiers that can have values of simple type. Let us see at example of enum type:

public enum Days
{

Monday,
Tuesday,
Wensday,
Thursday,
Friday,
Saturday,
Sunday

}


In example there are enum that has days of week names. The values of days by default are in range from 0 to 6.
A good beginning makes a good ending