1. Value Types

There are three general values types; Build-in Types, User Defines Types, and Enumeration

1.1 Build-in Types

The Build-in values types are derived from System.ValueType base type. We choose a specific value type based on the size of the value you need to store and the level of precision required on value. Bellow table (table 1) list the most common value types used in .NET





Table – 1 Common value type used in .NET 2.0





Important point in value type

The .NET runtime optimizes the performance of 32- bit integer values, so for frequently used integral variable like counters use Int32 or UInt32. Same way for floating point operation use Double. These types are more efficient since they are optimized by hardware.

For More details refer : http://msdn2.microsoft.com/en-us/library/hfa3fa08(VS.71).aspx

Nullable Value Types

The Nullable type is a new featured introduced in .NET 2.0. This allows a value type variable to hold null value (a privilege reserved only for Reference object in previous versions of .NET) and does not force the developer to initialize the variable to avoid possible confusion. This feature also allows you to check whether a variable has assigned a value or not.

Syntax for declaring a variable as Nullable (C#)


Nullable<bool> b = null;

OR

boll? b = null


Declaring a variable as Nullable enables two key members which are “HasValue” and “Value”. HasValue allows you to check whether a value has been set or not and Value member is used to access the value. Here is simple example that will test whether a Nullable bool variable is set or not

Example 1: to demonstrate Nullable value type
private void button1_Click(object sender, EventArgs e)
{
bool? b = null;
b = true; // comment this line for null test
if (b.HasValue)
{
MessageBox.Show("The value of B is : " + b.Value);
}
else
{
MessageBox.Show("The value of B is not set : ");
}
}

1.2 User Defined Types

Used defined types are also called Structures or Structs. In simple terms, structures are composite of other types that are logically grouped for simplicity. Please see the bellow example and it is self explanatory.

Example 2 : For structures

//Declare a Structure
struct box
{
public int length, width, height;
//override tostring function
public box(int ln,int wd ,int hg)
{
this.length = ln;
this.width = wd ;
this.height = hg;
}
// override function for toString
public override string ToString()
{
int area = length * width * height;
return area.ToString();
}
//operator new feature in .NET 2.0
public static box operator +(box arg1, box arg2)
{
box result = new box(arg1.length + arg2.length, arg1.width + arg2.width, arg1.height + arg2.height);
return result;
}
}


Calling function code

private void button1_Click(object sender, EventArgs e)
{
box box1 = new box(3, 3, 3);
box box2 = new box(4, 4, 4);
box box3 = box1+box2;
MessageBox.Show(box3.ToString());
}


The above structure represent simple box with length, width and height. Some of thing we need to know in structures.

1. Structure create instance in the stack not in heap.
2. You can not have explicit parameterless constructors
3. Structure can be instantiated with out the New operator
4. operator is new feature introduced in .NET 2.0


1.3 Enumeration

Enumerations are related symbols that have fixed values. Use enumeration to provide list of choices. Please seen the example 3 that provide list of choices for developer to choose size of a shirt.



Example 3: Enum to define shirt size.

enum shirtsize
{
xs,s,m,l,xl,xxl
}

No comments: