C# Data Types

As you have already known that while declaring a variable in C#, you have to specify the data type with the variable name. This is how you tell the compiler what type of data the variable will hold. Almost all programming language needs the concept of the data type. Since C# is a strongly typed language, it is essential to inform the compiler what kind of data these memory locations will hold. In this chapter, you will learn about the different data types that are supported by C#.

Data Types in C#

Every C# variable will be of any one of the three types. These are:
  • Value Type
  • Reference Type
  • Pointer Type

Let us discuss each of the types in details:

Value Type

When a variable can be assigned a value directly, they are termed as Value Type variables. Their origination comes from the System.ValueType class. They are the preliminary data types of the C# language.

  • int
  • float
  • char

As soon as a programmer declares a variable with a data type, the compiler allocates some memory (based on the number of bytes it takes) for storing the value.

Integral Type

C# allows you to define eight different types of integral variables. They provide support for 8-bit, 16-bit, 32-bit and 64-bit values with signed & unsigned modifiers. These eight types are mentioned below with their keyword, range, class name and default value.

ALIASTYPETYPE NAMESIZE(BITS)RANGEDEFAULT VALUE
shortsigned integerSystem.Int1616-32768 to 327670
sbytesigned integerSystem.Sbyte8-128 to 1270
Intsigned integerSystem.Int3232-231 to 231-10
longsigned integerSystem.Int6464-263 to 263-10L
byteunsigned integerSystem.byte80 to 2550
ushortunsigned integerSystem.UInt16160 to 655350
uintunsigned integerSystem.UInt32320 to 2320
ulongunsigned integerSystem.UInt64640 to 2630

Floating Point Type

Floating point data types in C# are of two types. These are:

  1. 32-bit single (7-digit) precision floating point type declared using the keyword float. For initializing any variable with a float, you have to mention a 'f' or 'F' after the value. For example: float g = 62.4f; In case you do not use the suffix, then compiler treats the value as double.
  2. 64-bit (14-15 digit) precision floating point type declared using the keyword double. For initializing any variable with double, you have to mention a 'd' or 'D' after the value. For example, double ks =8.403122d;

Decimal Types

There is another type of variable that is used for extensive calculations which are of 128-bit used for calculating huge economic data. It uses' or 'M' as the suffix; otherwise, the value will be treated as double.

Character Types

This is used for representing 16-bit Unicode character used for storing a single character.


Reference Type

A reference type is another form of the variable that does not hold the actual value or data, instead, it possesses a reference to any memory location that is assigned with a variable. This refers to a memory location

The built-in reference types provided by C# are:

  1. string
  2. object
  3. dynamic

All these will be covered in the later chapter of this tutorial.


Pointer Type

These types of variables are used for storing the address of any memory location which is of another type. Pointers are considered a separate data type kind because they do not hold the actual value or data; rather they are meant to store the actual memory location. The concept of pointers came in C# from C and C++.

The syntax of declaring pointers in C# is:

Syntax:
type* identifier;
Example:
char* str_name;
int* user_id;

Scroll Back to Top