Thursday, 9 October 2014

Datatypes in C#

There are three types of data types in c#.
  1. Value Type
  2. Reference Type
  3. Pointer Type
Value Type:

The value types directly contain data. eg. int, char, float, which stores numbers, alphabets, floating point numbers, respectively. When you declare an type, the system allocates memory to store the value.

 int i=10;

Reference Type:

Object Type

The Object Type is the ultimate base class for all data types in C# Common Type System (CTS). Object is an alias for System.Object class. So object types can be assigned values of any other types, value types, reference types, predefined or user-defined types. However, before assigning values, it needs type conversion.
When a value type is converted to object type, it is called boxing and on the other hand, when an object type is converted to a value type, it is called unboxing.
object obj;
obj = 100; // this is boxing

Dynamic Type

You can have any type of value in the dynamic data type. Type checking place at run-time for these type of variables.
Syntax :
dynamic <variable_name> = value;
For example,
dynamic d1 = 200;
In object type type checking take place at compile time and in dynamic type checking
take place at runtime. 

String Type

The String Type you can have any string value. The string type is an alias for the System.String class.
For example,
string str="Test String";

Pointer Type: 

Pointer type variables store the memory address of another type. These are like C or C++.
pointer type is:
type* identifier;
For example,
char* cptr1;
int* iptr1;

No comments:

Post a Comment