Data types in C#
# Data types are classified into two types –
1. Value types
2. Reference type
Value types store the data in stack which is the place where data store in fixed length such as int float etc.
1 Every program has its own stack and no other program shares it.
2 Stack works on the principal “first in last out” and it is under the control of operating system which doesn’t provide automatic memory mgmt but faster in access.
Reference type –
reference type are store on Heap memory which is the other place where data can be stored. C# support two predefined reference type objects and strings and .net the Heap is now more managed and called as managed Heap, which will be under the control of garbage collector.Nulleble Value types (2.0):-
it is a new feature that has been added in C# 2.0 which allows storing of null value in value types that is not possible in the version before 2.0, which gives more flexibility while communicating with data bases because under databases null values can be stores both in value types and reference types also.
Ex- string Str = null; //valid
Int x = null; //Invalid
Int? x = null; //valid
Note: - to declare a value type for storing null value we need to suffix the type with “?”. Implicitly typed variable (3.0) :- It is a new feature that has been added in C# 3.0, which allows declaring a variable using the keyword “var”. the type of declared variable is identified in run time depending upon the value we assign to the variable.
Var x = 100; //x is an int variable.
Var f = 3.14; //f is a float variable.
Var s = “Hello”; // s is a string variable.
Note- Declaring a variable here without assigning a value is not allowing.
Var y; // invalid.
Note :- get type is a predefined method that return the type of a given variable or object.
Boxing & Un-boxing :-
If at all a value type is assign to a reference type variable and sent to managed heap we called it as Boxing
int x = 100;
object obj = x; // BOXING
Un Boxing is reverse a boxing which allows reassign of refrance type back to corresponding vale type again but here an explicit type casting is required.
int y = (int)obj; // un - Boxing
0 Comments