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.

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#

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:

1

2

3

4

5

intsum;

chargender;

floatincome;//We also declare many variable of same data type in same line by separating commas.

intdifference,product,sum;

chargender,choice;

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:

1

2

3

intnumber1=5,number2=10;

charb=‘x’;

Or

1

2

3

4

5

intsum,product;

sum=5;

product=6;

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

1

2

3

intsum,number=3,product;

sum=2,product=4;

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

Code Example for Variables in C#

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

using System;

namespaceApplicationSpace

{

classProgram

{

staticvoidMain(string[]args)

{

inta,b;

a=12;

b=32;

intc=10;

charname='x';

doubley=2.322;

stringaddress="USA";

}

}

}

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:

1

intA=10;

1

Console.write(A);

Or

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

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.

1

2

3

4

5

intx=5;

inty=0;

y=x;//now value of y=5

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.