Variable in C#

Variable is a nothing but a memory location for storing value or we can say variable is a storage area.

Each variable in C# must be declared with specific data types, which determine the size and type of value variable can store.Variable is a storage area which store the values, values may be numbers, strings , floating values or arrays. Data type should be a int, string, char, float.

C# Importance Point

  • A variable is a name given to a value.
  • A variable can hold value of specific data types like : int, string, float etc.
  • A variable can be initialized after declaration or initialized and declared at the same time.
  • The value of variable can be changed at any time in a scope.
  • We can not declared multiple variable with same name within a scope.

In C# we use basic data types like : int, float, char, string and bool.

Data Types Data Types Example
Integer types int, uint, long, ulong, byte, sbyte
Floating types float , double
Boolean returns true or false
Decimal types decimal

Variable can be declared and initialized later in program or it can be initialized and the declared at the same time.

C# Variable declaration

<data_type> <variable_name>

Example :

int myno;

string name;

Here, int and string are data type and  myno and name are variable name

Variable Initialized after Declaration

<data_type> <variable_name>

<variable_name> = <value>

Example :

int myno;
myno = 10;

string name;
string = “meera academy”;

Above C# statement we declared first variable and then initialize it later in program.

In first line we declare variable myno with int data type and then assign values 10 to myno variable. Second line we declared string variable name and assign value ‘meera academy’ to name variable.

Variable Initialized with Declaration

We can initialize variable at the same time of declaration.

<data_type> <variable_name>  =  <value>

Example :

int  a= 10;

string name=”meera academy”;

variable in c# .net

C# variable initialize with declaration.

Above statement we declared int variable a with values 10 and string variable name with values ‘meera academy’ at the time of declaration.

Multiple Variable Declaration

Multiple variable declared with same data type with separated by commas with a single line.

int a, b, c =10;

string name, city ;

Here, we declared multiple variable a,b and c with int data types and assigned with values 10;

Assigned variable  value to another variable

The value of a variable can be assigned to another variable of the same data type. Both variable must have same data type.

int  a = 10;

int  b;

b = a;

Above variable example we declared int a variable with values 10 and int b variable, later we assign a variable to b variable (a=b). Now b values is same as a variable value =10.

Invalid variable assignment

int  a = 10;

string  name = a;

Above statement will give compile error because integer variable a values can not be assigned to string variable.

Leave a Reply

Your email address will not be published. Required fields are marked *

Captcha *