C# Tutorial C# Advanced C# References

C# - Data Types

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
  • Reference Data Types
  • Pointer 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 table below 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

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.

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. The 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 variable of a value type is converted to object, it is said to be boxed. When a variable of type object is converted to a value type, it is said to be unboxed.

object obj;
int x;

obj = 50;       //boxing
x = (int)obj;   //unboxing

Dynamic Type

The Dynamic Type is used to any type of value to a variable. It is similar to object type except that type checking for object type variables takes place at compile time, whereas that for the dynamic type variables takes place at run time. Syntax for declaring a dynamic type is given below:

//syntax for declaring dynamic type
dynamic <variable_name> = value;

//for example
dynamic x = 50;
dynamic y = "Hello World";

String Type

The String Type is used to store a string values to a variable. It is an alias for the System.String class and derived from object type. The value for a string type can be assigned using string literals in two forms: quoted and @quoted. Consider the example below:

String str1 = "Hello World";
String str2 = @"Hello World";

The user-defined reference types are discussed in later sections.

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;