C# Data Types And Variables With Examples

This Tutorial Explains C# Data Types And Variables. You can Learn to Define, Initialize and Declare a Variable Along with Various Data Types in C#:

C# Program Structure and Basic Program were explained in detail in our previous tutorial.

This C# tutorial will educate you all about Data Type And Variables in C# with simple examples for your easy understanding.

=> Check Out The In-Depth C# Training Tutorials Here

C# Data Type And Variables

C# Data Type

Data types are the key components of any programing language as we have already discussed in the previous tutorial that a data type must be declared for a variable.

A data type communicates with the compiler informing it about the type of data that a particular variable can hold inside it. C# has several data types built inside it like Boolean, Integer, Float, Decimal, etc.

Whenever a variable is declared with a data type, the system allocates some memory space to store its values.

class Program
 static void Main(string[] args)
string stringVariable = "Some value";

Each of the data types in C# has a specified range of values, for if a variable is declared as integer data type then it can hold any specific value between -2,147,483,648 and 2,147,483,647.  Similarly, the other data types have their own set of the value range.

Let’s have a look at some of the values and their ranges.

TypeRepresentsRangeDefault Value
stringA series of characters
charA Unicode character
objectObject type
boolBoolean valueTrue or FalseFalse
byte8-bit unsigned integer0 to 2550
decimaldecimal values with 28-29 significant digits(+ or -)1.0 x 10e-28 to 7.9 x 10e280.0M
double64 bit double-precision floating point type(+/-)5.0 x 10 raise to -324  to (+/-)1.7 x 10 raise to 3080.0D
int32 bit signed integer type-2,147,483,648 to 2,147,483,6470
float32 bit single-precision floating point type-3.4 x 10 raise to 38  to + 3.4 x 10 raise to 380.0F
long64 bit signed integer type-9,223,372,036,854,775,808 to 9,223,372,036,854,775,8070L
uint32 bit unsigned integer type0 to 4,294,967,2950
short16 bit signed integer type-32,768 to 32,7670
ulong64 bit unsigned integer type0 to 18,446,744,073,709,551,6150

As we can see in the above table that each of the data types excluding String, Character, and Object has a value that lies within a pre-defined range. The C# compiler will give an error if the data type exceeds its pre-defined range of values.

For Example, if we provide a value less than 0 or greater than 255 to a byte data type then it will throw an error. Data types are again classified into a Reference type, Value type, and Pointer type. This depends upon the variable characteristics as whether it is storing its data or is it just pointing to a specific data at a memory location.

C# Variable

Variable is the name that we provide to the storage location where we store our values. Each of the variables in the C# environment has its own type, which is further defined by its characteristics like type and size of value it can store. It also defines the set of operations that can be performed by the program to manipulate that value.

Following are the basic value types in the C# that can be further categorized into data types:

  • Integral types
  • Floating
  • Boolean
  • Decimal
  • Nullable

A variable in C# is always defined by using a data type. Thus, the syntax for a variable declaration will be as shown below.

<data type> <variable name>;

The data type definition must be a valid C# data type that includes float, int, double, char or any other data type that a user may define. The variable name is the name chosen by the user to identify variables. A variable can be initialized and declared at the same time or it can be initialized first and can be declared later.

Example: Here, we are initializing variables first and then we are declaring it in the latter part of the program.

string val;
val = "Hello World!!";

Or, we can initialize and declare a variable at the same instance.

string val = "Hello World";

Here we are initializing a variable “val” with string data type and at the same time we are also assigning it a value i.e. “Hello World”

Multiple variable declarations are also possible in a single line by separating them with commas.

int i, j, k, l;

While declaring multiple variables, you can also put them in multiple lines for better readability. Even when they are spread across, multiple line compiler will consider them to be in the same command line until it encounters semicolon “;”.

For Example:

int i,

We can also define one variable with an assigned value and can define another variable assigning the first variable to it.

int i = 20;
int j = I;

A variable should be assigned with a value before its usage or else the compiler will show an error.

You can also accept values in a variable directly from a user by invoking Console class in the System namespace and using its ReadLine() function.


string read;
read = Console.ReadLine();

In the above example, we declared a string read and then we used the read variable to store the data input from the user by using Console.ReadLine command. Console.ReadLine command accepts the data only in string format so, if we want to store the value in any other data type variable then we have to convert the value into the desired data type before assigning it.

Classification Of Data Types

In the above section we have categorized the data types based on the type of value they accept and the quantitative memory they use for storing the data type. Further data types can be broadly divided into two categories based on how the value is stored in their memory.

  • Value Type
  • Reference Type

#1) Value Type

Value type data types are the variable that holds a data value within its own designated personal memory space. Hence, these data types directly hold their values.

int i = 20;

Here the integer variable “i” is directly holding the value of 20.

#2) Reference Type

In contrast to the value type, a reference type doesn’t directly hold values. Instead of directly holding the value it holds the address where the value might be stored. In simple words, a reference type variable just holds a reference to a particular memory location that may hold the required data.

Some of the reference type data include string, arrays, class, etc. Hence, if any changes are made to the data then the other variable will automatically inherit the new changed value and if there are no values assigned to the reference type then by default it contains a null value.

There are three different reference type:

  1. Object type
  2. Dynamic type
  3. String

#1) Object Type

The object type is regarded as the base class for all the objects in the C# programming languages. The object types can be assigned with the values of any of the other types including value type, user-defined types or even any other reference type.

object obj = 25;

#2) Dynamic Type

Dynamic type can be used to store any dynamic variable or data type. The type check for a dynamic variable is carried out during run time instead of at the time of compilation.

dynamic dyn = 123;

#3) String Type

String type is one of the most widely used data types. It is used to assign a series of character values to a variable. The string keyword refers to the object type of the System. String class.

String strng = “hello world”;

Data Type Conversion

Data type conversion is converting one data type into another type. Sometimes it is also called as Type Casting in C#.

There are two types of Conversions:

  • Implicit Conversion
  • Explicit Conversion

(i) Implicit Conversion

Implicit Conversion is easier to use as it uses no syntax as the conversion is type-safe and doesn’t result in loss of data. It doesn’t result in a loss of data and doesn’t even require any syntax. In an implicit conversion, a smaller data type is converted into a larger data type.

For example, the conversion of integer to double. Even if we convert integer type data into a double type, no loss of data occurs as double being a larger type can easily hold a smaller type variable.

int a = 10;
double b = a;

(ii) Explicit Conversion

Explicit Conversions are carried by a user using type conversion syntax. For explicit conversion, a cast operator is required. This type of conversion is mostly used for converting larger types into smaller types or for converting base class to its derived class.

Due to the difference in the data structure, some data loss may occur during conversion and may even show an error. This is not a type-safe conversion.

int a = 10;
double b = a;


In this tutorial, we learned about Variables and Data Types. We discussed how we can define a variable. We also learned to initialize and declare a variable. We saw the different data types that can be used to declare a variable.

Each data type has its own set of ranges inside which the value is declared and if we don’t have a declared value then a default value is stored. We also saw how one set of data type can be converted into another by using Implicit and Explicit conversion.

Implicit conversions are type-safe as no data loss occurs during conversion, mostly because a smaller data type is being converted into a larger data type. On the other hand, the explicit conversion may result in loss of data as the larger data type is converted into a smaller data type.

In the next tutorial, we will discuss in detail about data type and we will try to resolve some data type into another.

=> Watch The Full C# Training Series Here