C# Data Types

C# Data Types

The C# language comes with a set of Basic data types. These data types are used to build values which are used within an application. Let’s explore the basic data types available in C#.

Data types specify the type of data that a valid C# variable can hold. C# is a strongly typed programming language because in C#, each type of data (such as integer, character, float, and so forth) is predefined as part of the programming language and all constants or variables defined for a given program must be described with one of the data types.

The CSharp type system contains three Type categories. They are Value Types, Reference Types and Pointer Types.

The Value Types store the data while the Reference Types store references to the actual data. Pointer Types variable use only in unsafe mode. The Value Types derived from System.ValueType and the Reference Types derived from System.Object.

The main difference between Value Types and Reference Types is that how these Types store the values in memory. Common Language Runtime (CLR) allocates memory in Stack and the Heap . A Value Type holds its actual value in memory allocated on the Stack and Reference Types referred to as objects, store references to the actual data. In C# it is possible to convert a value of one type into a value of another type . The operation of Converting a Value Type to a Reference Type is called Boxing and the reverse operation is called Unboxing.

Data types in C# is mainly divided into three categories:

  1. Value Data Types
  2. Reference Data Types
  3. Pointer Data Type

 

1 - Value Data Types

The value data types are integer-based and floating-point based. C# language supports both signed and unsigned literals.

In C#, the Value Data Types will directly store the variable value in memory and it will also accept both signed and unsigned literals. The derived class for these data types are System.ValueType

There are 2 types of value data type in C# language:

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

The memory size of data types may change according to 32 or 64 bit operating system.

The following table lists the value data types in c# programming language with memory size and range of values.

Data Type.NET TypeSizeRange
byteByte8 bits0 to 255
sbyteSByte8 bits-128 to 127
intInt3232 bits-2,147,483,648 to 2,147,483,647
uintUInt3232 bits0 to 4294967295
shortInt1616 bits-32,768 to 32,767
ushortUInt1616 bits0 to 65,535
longInt6464 bits-9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
ulongUInt6464 bits0 to 18,446,744,073,709,551,615
floatSingle32 bits-3.402823e38 to 3.402823e38
doubleDouble64 bits-1.79769313486232e308 to 1.79769313486232e308
boolBoolean8 bitsTrue or False
decimalDecimal128 bits(+ or -)1.0 x 10e-28 to 7.9 x 10e28
DateTimeDateTime-0:00:00am 1/1/01 to 11:59:59pm 12/31/9999

 

2 - Reference Data Type

The reference data types do not contain the actual data stored in a variable, but they contain a reference to the variables.

If the data is changed by one of the variables, the other variable automatically reflects this change in value.

There are 2 types of reference data type in C# language:

  • Predefined Types - such as Objects, String.
  • User defined Types - such as Classes, Interface.

The following table lists the reference data types in c# programming language with memory size and range of values:

Data Type.NET TypeSizeRange
stringStringVariable Length0 to 2 billion Unicode characters
objectObject--

 

3 - Pointer Data Type

The pointer in C# language is a variable, it is also known as locator or indicator that points to an address of a value.

To get the pointer details we have a two symbols ampersand (&) and asterisk (*):

  • ampersand (&): It is Known as Address Operator. It is used to determine the address of a variable.
  • asterisk (*): It also known as Indirection Operator. It is used to access the value of an address.

Following is the syntax of declaring the pointer type in the c# programming language:

type* identifier;

Following is the example of defining the pointer type in the c# programming language:

int* a;
int* b;

Example:

int* p1, p;   // Valid syntax
int *p1, *p;   // Invalid syntax
01. Symbols used in pointer

The following table lists the detail of different type pointer symbols available in the c# programming language:

SymbolNameDescription
&Address OperatorDetermine the address of a variable.
*Indirection OperatorAccess the value of an address.
02. Declaring a pointer

The pointer in C# language can be declared using * (asterisk symbol).

int * a;  //pointer to int      
char * c; //pointer to char