Monday, April 26, 2010

Value type

Value type are variables that create directly in the memory which is called as stack, in the run time create, update and remove them quick and less overhead.
Three general value types:-
1. Built-In type,
2. User Defined (Structs or Structure),
3. Enumeration

Those values are defined from system.value base type

Built-in type Value Type:-
> All built-in numeric types are value type,
Type size range use for
System.SBytes (SByte/sbyte) 1 -128 to 127 signed byte values
System.Byte (Byte/byte) 1 0-255 Unsigned bytes
System.Int16 (Short/short) 2
Sytem.Int32(Integer/int) 4 whole number and counter
System.UInt32 (UInteger/uint) 4 all positive number
System.INT64 (Long/long) 8 large whole number
System.Singal(Single/float) 4 Floating point number
Double/double 8 large floating point numbers
Decimal/decimal 16 financial and Scientific calculations ..

• runtime optimized 32-bit integer (int32/UInt32) and floating point operations double is most efficient because those are optimized by hardware.

> Non – numeric value types -:
Char/char 2 single Unicode character
Boolean/bool 4 True/false
System.IntPtr platform dependent Pointer to memory address
System.DateTime 8 Moment in time

> Value type has implicit constructor assign default constructor as null or 0
> C# are case sensitive
Nullable - this is new for .net 2.0
Declara value type as nullable
Nullable b = null or bool ? b = null

Use HasVale to check whether or not a vale has been set.
If (b.HasVale){ Console.writeLine(“b is vale {0}.” , b.value); }
else {Console.WriteLine(“b has no value”);}

User Defined Type (Struct)

Structure behave identically as a Class (class reference type struct value type)
Ex -: following example create a type of circle which given minimum and maximum number set by constructor
// ----------------------------
static void Main(string[] args)
{
cycle degree = new cycle(0, 359);
cycle Quater = new cycle(1, 4);

for (int i = 0; i < 8;i++ )
{
degree += 90;
Quater += 1;
Console.WriteLine("degree ={0}, and Quater ={1}", degree, Quater);
}
Console.Read();
}
struct cycle
{
int val;
int min;
int max;
public cycle(int minV, int maxV)
{
val = minV;
min = minV;
max = maxV;
}
public int Value
{
get {return val;}
set
{
if(value >max)
{
val = min;
}
else
{
if (value < min)
{
val = max;
}
else
{
val = value;
}
}
}
}
public override string ToString()
{
return Value.ToString();
}
public int toInteger()
{
return Value;
}
public static cycle operator +(cycle arg1, int arg2)
{
arg1.Value += arg2;
return arg1;
}
public static cycle operator -(cycle arg1, int arg2)
{
arg1.Value -= arg2;
return arg1;
}
}
//----------------------------


struct cycle
// can be chane struct to class it automaticaly be a reference type
{
// declar varible , defind method and operations
}
Above example cycle value type can be easily convert to reference type changing struct key word to class key word then cycle class be allocate in the heap rather than 12 byte on the stack,
 Structures are more efficient than class ,
 Following criteria should be meet for structures
o Logically represent single value;
o Has an instance size less than 16 byte;
o Will not be change after creation;
o Will not be cast to the reference type;

Create Enumeration

This is specific type symbol that have fixed value;
Ex: enum Titles : int {MR,MRS,MS,DR};
Titles t = Titles.DR;
 This is simplify coding and code readability

No comments:

Post a Comment