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

Java Program to find the sum of the Largest Forward Diagonal

in this tutorial, we will write a java program to find the sum of the Largest Forward Diagonal in an Arraylist (matrix). Java Program to …

C Program to search an element in an array using Pointers

A separate function( search_function()) will be created where the array pointer will be declared and the searched element along with the size of an array …

C Program to find the sum of the digits of a number using recursion function

This C program calculates the sum of digits of a given number using recursion. Here’s a concise explanation: Function Definition: sumDigits(int n) This function calculates …

C program to find factorial of a numberĀ using Ternary operator with Recursion

Recursion refers to the function calling itself directly or in a cycle. Before we begin, you should have the knowledge of following in C Programming: …

C Program to Add Two Numbers Using Call by Reference

The program takes the two numbers from the user and passes the reference to the function where the sum is calculated. You may go through …

Find the output ab, cd, ef, g for the input a,b,c,d,e,f,g in Javascript and Python

In this tutorial, we will write a program to find a pairs of elements from an array such that for the input [a,b,c,d,e,f,g] we will …