A variable is like a container in a C# program in which a data value can be stored inside the computer's memory. The stored value can be referenced using the variable's name.
The programmer can choose any name for a variable as per C# naming conventions, such as;
num
, Num
, and NUM
are treated as three individual variables.char
data type must be enclosed in single quotes, but character strings of the string
data type must be enclosed between double-quotes.To create a new variable in a program it must be declared, specifying the type of data it may contain and its chosen name. The following is a variable declaration syntax.
data-type variable-name;
The following number
variable of int
type is declared in the first line and then initialized in the next line.
int number; // Declared number variable
number = 100 ; // Initialized.
The following body
variable of the float
type is declared and initialized simultaneously.
float body = 98.6f ; // Declared and initialized.
Multiple variables of the same data type can be created in a single declaration as a comma-separated list.
data-type variable-name1 , variable-name2 , variable-name3;
The following multiple variables of the int
type are created in a single declaration.
int a, b, c;
a = 100;
b = 200;
c = 300;