Variables in C#

Common Questions Asked about variables in C#

In any programming language, the variable is the most important part of the program, and the data which we use is the program is stored and handle with variables. In this topic we cover the following points:

  • What are the variables?
  • How many types of variables in C#?
  • How to declare a variable?
  • How to initialize a variable?
  • How to read and write variables data in C#?
  • How to swap variables values?

What are Variables?

Variables are used to store data in programming languages and almost every programming language use variables to handle and store data of different types. They are the primary part of a program.

”In computer programming, a variable is a storage location paired with an associated symbolic name (an identifier), which contains some known or unknown quantity of information referred to as a value.”

Variables are the symbolic name for information, and they store some information as values.

Normally a variable has the following syntax:

[Data type] [Variable name]

Data types have two types Predefine and User-Defined. But this time we only deal with Predefine Data types to understand the variables. Variables are nothing, just a name given to storage and it only stores the data of the type defined by its data type.

Types of variables in C#

In every language variables have some specific type, size and storage size. In C# they also contain different size as compare to other languages.

Now take a look on variable types in C#.

Integer type, Character type, floating point, decimal numbers, null data, true and false etc. etc.

For integer type variables we use int data type, as for characters (letters) we use char data type in C#. C# also have string data type to store string type variables and floating points numbers have float data type.

Declaration of variables in C#

In C# variable declaration syntax is similar to C++, C and Java.

<data_type> <variable_name><terminator>

Data type: Both pre-defined or user define like int, char, float, string

Variable Name: It’s any meaningful name you want to give like product, difference, sum, temp

Terminator: As every C# code line end with semicolon, so it also terminates with a semicolon.

Some Valid variable definitions are given below:


Initializing a variable in C#

We define variable to store some value in it so giving some value to a variable of some data type in called initializing variable. There are two methods of initializing one is at the time of definition, and other is later on after definition. The syntax for initiating and declaring a variable in C# is following:

<data_type> <variable_name> = <value> ;

Some examples of initializing a variable are:


We can also mix both of them according to our use like

It’s a good programming practice to initialize a variable in the start, and if you have nothing like a value, you must initialize it with null or 0. As If you don’t assign any value C# will automatically assign some dummy value to it.

The example below in fig show the definition and initialing of variables of different data types:


variables in C# visual studio
variables in C# visual studio

Code Example for Variables in C#


Read and Write Variables values

To write the value of variable on console in C# we use Console.Write();

For example, we have, and integer type variable name ‘A’ and it contains integer value 10, and now we want to show it on console so for this the code is something like below:


Console.Write(“Value of A is= {0}”, A);

display variable on console C#
display variable on console C#

Swap Variables Values

As variables are used for the storage of data and sometimes we have to transfer that data or value from one variable to another, so we swap or assign the value of one variable to other.

swap variable values in C#
Swap variable values in C#

When we have two variable, and we want to interchange value of both with each other, then we use a temporary variable to store temporary value of one variable and then change the value to other.

interchange variable values in C#
Interchange variable values in C#


Output of swapped variable values in C#
Output of swapped variable values in C#