Facebook Page Twitter Page LinkedIn Page
× C# Tutorial C# Advanced C# Resources

One of the most important parts of learning any programming language is to understand what are the available data types, and how data is stored, accessed and manipulated in that language. Based on the data type of a variable, the operating system allocates memory to the variable and decides what can be stored in the reserved memory.

There are three types of data types in C# which are categorized below:

  • Value Data Types
  • Pointer Data Types
  • Reference Data Types

Value Data Types

The value data types store the value directly in the memory and it accept both signed and unsigned literals. There are two types of value data types in C# language:

  • Predefined Data Types - such as Integer, Boolean, Float, Double etc.
  • User defined Data Types - such as Structure, Enumerations, etc.

The below table describes the data types, size and it's range:

Data TypeSizeRange
bool1 bytestrue or false
char16 bit Unicode characterU+0000 to U+FFFF
sbyteSigned 8-bit integer-128 to 127
byteUnsigned 8-bit integer0 to 255
shortSigned 16-bit integer-32,768 to 32,767
ushortUnsigned 16-bit integer0 to 65,535
intSigned 32-bit integer-2,147,483,648 to 2,147,483,647
uintUnsigned 32-bit integer0 to 4,294,967,295
longSigned 64-bit integer-9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
ulongUnsigned 64-bit integer0 to 18,446,744,073,709,551,615
float4 bytes±1.5 x 10-45 to ±3.4 x 1038, ~6-9 digits precision
double8 bytes±5.0 x 10-324 to ±1.7 x 10308, ~15-17 digits precision
decimal16 bytes±1.0 x 10−28 to ±7.9228 x 1028, 28-29 digits precision

The memory size of a data type might be different as shown in the above table, depending upon platform. To find out the size of a data type, sizeof() function can be used.

using System;
class MyProgram {
  static void Main(string[] args) {
    Console.WriteLine("Size of float: {0} bytes", sizeof(float)); 
    Console.WriteLine("Size of int: {0} bytes", sizeof(int));
    Console.WriteLine("Size of char: {0} bytes", sizeof(char));

The output of the above code will be:

Size of float: 4 bytes
Size of int: 4 bytes
Size of char: 2 bytes

Pointer Data Types

A pointer data type stores address of another variable. Pointers in C# have the same functionality as in C or C++. Syntax for declaring pointer is given below:

int *p1;     //pointer to an integer
float *p2;     //pointer to a float

//pointer p1 which stores address of Var
p1 = &Var;

Reference Data Types

The reference data types do not contain the actual data stored in a variable, but they contain a reference to the variables. With reference types, two variables can reference the same object. If the data is changed by one of the variables, the other variable automatically reflects this change in value. There are two types of reference data types in C# language:

  • Predefined Data Types - such as Object, String, Dynamic etc.
  • User defined Data Types - such as Class, Interface etc.