Tuesday, May 28, 2013

Value Type

Value Type variables are those which have a separate memory segment allocated for them, when a value type variable is copied to another value type variable each variable has its own memory segment and changes done to one of the variable does not affect the value of the other.

All value type variables are implicitly derived from System.ValueType

By default all parameters passed to a function are of value type, hence any change happening to the parameter variables in the called function does not affect the value of the variable passed from the calling function. We can force the parameters to be passed by reference by adding a ref keyword in front of the parameter.

All Value Type variables are stored in the stack, which is a first-in-last-out data structure, memory management with stacks is simple and efficient, all the required objects are pushed into the stack while executing a function/method, and once the method execution is completed the entire stack is cleared.
Structs are value type
All primitive types like int, string, bool etc are Value Types.

All value types have an implicit default constructor which assigns the default value for the value types, hence if a value type is declared without initializing it takes the default value automatically.

For example, the following int declaration automatically assigns the value 0 to the integer variable.

int nId;

Search Flipkart Products:

No comments: