C# Variables (initialization, declaration, syntax and example)4 min read

Variables are like a storage unit that stores the values of various data-types present in a C# programming language. The types are different in C# and accordingly, the memory sizes are allocated to those types to store data values.

Consider an example where you have declared a variable of type int that means that the int variable will be allocated to a memory where it will integer values and some specific operation can be performed.

And for operation, consider the variable int that you described and since it is an integer the operation like addition and subtraction, multiplication, or modulus is performed on those integer operands.

C# consists of various types variables provided by its data-types:

  • int: It stores integers values such as 65 or -65.
  • double: It stores floating-point numbers such as 15.34 or -15.34.
  • char: It stores the character in a single-quotes such as ‘s‘.
  • string: It is for storing the string in double quotes such as “simple code
  • bool: It states two values: true and false.

Like variables in C, C++, java or any other programming language, the variable is declared before use and can be modified at runtime within any section of the code depending upon its types.

Syntax:


How to declare variable in C#?

It is simple to declare a variable and it must be declared before the use of that variable. You need to specify the data-type for the variable and also give it a unique name.

As mentioned above data-types can be of any type: int, float, double, char, string or bool, according to the need.

Let us see an example how you can declare it in a program.


How to initialize a variable in C#?

Initialization means giving the variable an initial value to start with. We can initialize the variable at the declaration part such as: int number = 45;.

The syntax is simple, first, start with the data-types and unique name the same as a declaration and then assign it a value with equal sign ‘=’.

The data value is always assigned at the right-hand sign of the assignment operator ‘=‘. Let us see some valid examples of how you can initialize a variable.

In the above example as you can see the first line contains two initialization which is possible considering you ant the two variable n you program to be an integer type.


User Inputs

Sometimes in a program you need to the value from the user and store it in a variable and use those variable at run time in a programs.

To accept the inputs from the user, C# provides a particular function named as ReadLine() that take the inputs and store it in a variable. It is present in the console class in the System.

How to use it in a program:

The above example specifies that the variable is declared and it takes the user input and places it in a number variable. Conversion of inputs to integer data-type is done by Convert.ToInt32 and acceptance of input is completed by Console.ReadLine() in the program.


C# defines two kinds of expressions:

  • lvalue: The expression may appear on either on the right-hand side or the left-hand side of the assignment operator.
  • rvalue: The expression can only appear on the right-hand side but not on the left-hand side of the assignment operator.

Variables are lvalue expression as it can appear on the right-hand side as well as on the left-hand side of an equal sign’=’ in a program. However numeric values are rvalues as it can only be assigned to the right-hand side of the ‘=’ value.

This is because the assignment operator(=) always assigns the right-hand side value to the left-hand side and we cannot assign something to a numeric value that is why they are the rvalue.

Look at the below example that shows which are valid and which are not.

There are few rules you need to follow which naming the variables such as it must be unique, it has to be letters, digits, or the underscore _. Variables are case sensitive that is Number and number are considered different variables.


MORE

Find a pair with the Given Sum in an Array in Java

In this tutorial, we will write a java program to find the sum pair in an array. Before that, you may go through the following …
Read More

C++ Program to display Pascal Triangle

In this tutorial, we will write a C++ program to display pascal’s triangle. Before that, you may go through the following topic in C++. for …
Read More

C++ Program to Calculate Sum of Geometric Progression

In this tutorial, we will learn and write a program to find the sum of GP series in C++. Before that, you may go through …
Read More

C++ Program to find the Transpose of a Matrix

In this tutorial, we will learn and write a program to find the transpose of matric in C++. Before that, you may go through the …
Read More

How to Split a String in C++

Let us learn how to split string in C++ programming language. We will look at a few ways to split a string. The splitting of …
Read More

C++ Program to calculate Average of Numbers using Arrays

In this tutorial, we will learn and write a C++ program to find the average of numbers using an array. To understand the coding, you …
Read More