C# Data types

C# Primitive Data types :

Any data types directly supported by the compiler are called primitive types. Primitive types map directly to types that exist in the base class library.

These basic data types are used across programming language compilers regardless of managed code or unmanaged code, since it is essential to meet needs of storing the basic data(Numeric, character, Bool) in application development.

Let us see Primitive Data types of C# :























The CLR supports two different types
  •    Value Type
  •    Reference Type  
Value Type :

The variable representing the instance doesn’t contain a pointer to an instance; the variable contains the fields of the instance itself. Value type instances are usually allocated on a thread’s stack

Value types are derived from System.ValueType

1. Value types holds the value itself and values are allocated in stack.

2. Being represented in two forms (boxing & unboxing).

3. You can't create new value type which derives from any of the System.ValueType, because all the ValueType in framework is sealed.

4. Need not to use 'New' Keyword explicitly, if you want to use the default constructor of value type then use 'New' keyword.

Ex:-
int i = 5; //value is 5
int i = new int(); // value is 0
Here Default constructor of System.int32 type will assign the 0 as default value to the variable.

5. Types are declared using struct keyword are always ValueTypes.

Note :
Ultimately System.ValueType is derived from System.Object, that is why both types share common methods (ToString, GetType, GetHashcode). However in ValueType, Equals and GetHashCode are overridden for specific value implementation.

Reference Type :

The variable which holds reference (address)to object which is allocated in heap rather having object as value itself called reference types.

Following are examples of reference types.

"System.String, System.Delegate, System.Exception, System.Object"

And the types which are all derived from System.Object called reference Types except System.ValueTypes.

1. Reference type variable holds the address of object allocated in heap.

2. Stored in heap and managed by GC.

3. Attempting to use a null reference type variable causes a NullReferenceException exception. Indicating that the reference type variable doesn't currently point to a valid object.

4. Instance of the reference type must be initiated using 'New' keyword. The new keyword returns a reference to the object allocated on the heap, not the actual object itself.
But the reference variable is stored on the Thread stack for further use in your application.

5. Types are declared using class are reference types

Tweaks :

if value of the reference variable copied to another variable, then both of them points to one object which is allocated on heap.


The obj1 And obj2 Reference type variables are having same address as pointing to single object in heap. Operation on either of variable's data (object on heap) make impact on the other one.

In case of ValueType variable, the data will be copied to another variable. Both of them have separate copies.


Now i and j ValueType variables hold separate copies of data. Manipulating the variable j's value will not impact on variable i.

Thanks for reading.. Catch you in next post.

Comments

Popular posts from this blog

Checking Overflow in C#

Exam 70-486 : Developing ASP.NET MVC Web Applications