Wednesday, October 3, 2012

TYPES IN C#

C# is a strongly typed language. Each variable and constants has type. A type has following information:


- It specifies the storage space that the type occupies.

- The location of the memory of that type occupies.

- The base type that it inherits from.



There are two built in types in C#.


1. Value Type

2. Reference Type.



 

VALUE TYPE



- Value type derives from System.ValueType which derives from System.Object.

- Value types directly contains their values.

- When you copy from one value type variable to another it copies all the data. So if you change one variable it does not affect another variable.

- Value Type are stored in Stack.

- Value type has default value. For eg: int i; So i has default value of 0.

- Runtime can create, delete, remove the value type quickly. So no garbage collection needed.

- It does not contain null values unless you can make it as nullable type.



There are three categories in value type.


1. Built in Type

- Integral type (sbyte, byte, char, short, ushort, int, uint, long, ulong)

- Floating point type (float, double)

- Decimal type (decimal)

2. Bool

2. Struct , Enum





REFERENCE TYPE


- Reference Type is derive from System.Object

- Reference Type variable stores address of their data.

- It has the pointer to points to the data.

- When you copy from one reference type variable to another it copies only the reference. So if you change one variable it will affect another

- Reference types are stored in the Heap.

- Reference types always has null values.

- The memory used by the reference type will handled by Garbage Collection

- Class, Interface, Object, String, Arrays, Stream, Exception, Delegate, StringBuilder are the reference type.



No comments:

Post a Comment