C#.net Data Types

Data type arrrowType of data

Data type specifies size of the data and type of data.

C#.net data type are divided into two types

Value Type

Int  i = 0

I arrrow10  (7000)

C language  C#

Reference Type 

Int * p = i

parrrow 7000  (9000)


Stack                       Heap

90%                            10%

10%                        90%


Value Type

Value type will be created at compile time and these will be started in STACK memory.

Value types holds the data directly

No default value for value type

GC (Garbage collector) cannot access stack memory . that is GC can not clears stack run time


Pre-defined data types


Enum’s (Enumerator)

Reference Type

Reference Type will be created at run time and these will be stored in HEAP memory.

Reference Type holds the reference but not the data.

Reference Type holds the default value.

GC can access HEAP memory.

Examples :-






Var (newly introduced)


Working with Predefined Data types 

These are divided in to 4 groups

Group 1 :- Integral Data type

.bytearrrow 1 byte              0 arrrow255

.S byte arrrow1 byte             -128 to +127

.Short arrrow2 bytes              -32768  to +32768

.U short arrrow   2 bytes                 0 – 65535

Int arrrow 4 bytes                          ~ +- 214 cores

U intarrrow 4bytes            0 -~ 429 cores

Long arrrow8 bytes                    + 2^63

U Long  arrrow 8 bytes     0 – 2 ^ 64 -1

Group 2 :- Floating Related Data Types

*float 4 B

*double 8 B

*decimal 16 B

Group 3 :- character Related Data types

 .char 2 B :  char holds UNICODE  (universal coding) characters which are required for globalization.

.String 16 B : 4 GB

Group 4 :- other Data type

.bool 1 B arrrow true/ false

.Data time arrrow 12 B

1/1/0001  to 31/12/9999


Syntax to declare a variable 

<Data type>  var  name = value;

 Note:- In C# .net always variable name need to be started either with alphabet, underscore, @ symbol

Obs :-

Int a =90;

Int – a = 90;

Int @a = 90;

Int *a = 90;

Int 2a = 90;

Int if  = 90;

Example on Data Types

Open console Application project .

Write the following code in main method.

Int  I = 90;

Console. Write Line (i);

Float x = 4.0 f

Console. Write Line (x);

Byte b1 = 10, b2 = 20;

Int c = b1 + b2;

Console. Write Line (c);

Console. Write Line ();

Execute the project :(f5)


value types must be initialized before using

float X = 4.0 gives an error


By default 4.0  will be treated as double;

Solution: -  float x = 4. 0 F  (or) float  X = 4. 0f;

A number, which is post fixed with f @ F is called as float.

A number, which is post fixed with d @ D is called as double.

A number, which is post fixed with m@ M is called as decimal.

Byte c = b1 + b2, gives an error + operator returns the result in the format of int.