Variables in C# are named storage locations used to store data that can be accessed and manipulated during program execution. Each variable is declared with a specific data type and name and can optionally be initialized with a value.
- Data Type: Specifies the type of data a variable can store, such as int, double, or string.
- Variable Name: Identifies the variable and must follow C# identifier rules.
- Value (Optional): Can be assigned during declaration or later in the program.
using System;
class Geeks
{
static void Main()
{
// Declaring and initializing variables
// Integer variable
int age = 25;
// String variable
string name = "GeeksforGeeks";
// Double variable
double salary = 50000.50;
// Displaying the values of variables
Console.WriteLine("Age: " + age);
Console.WriteLine("Name: " + name);
Console.WriteLine("Salary: " + salary);
}
}
Output
Age: 25
Name: GeeksforGeeks
Salary: 50000.5Declaration
A variable declaration in C# specifies the data type and variable name.
Datatypes Variable_Name;

A variable declaration consists of two main components: the data type and the variable name.
Here, int is the data type and count is the variable name.
Initialization
Initialization means assigning an initial value to a variable.
A variable can be declared and initialized at the same time.
int age; // Declaration
age = 25; // Assignment
int marks = 90; // Declaration + initialization

Here:
- int is the data type.
- age is the variable name.
- 25 is the value assigned to age.
Example of Variable Initialization
Here is an example of initializing variables of different types such as float, int, and char.
using System;
class Geeks
{
static void Main()
{
// Declaring and initializing variables
// Initializing float variable
float si = 5.5f;
// Initializing integer variables
int t = 10;
int s = 20;
// Initializing character variable
char var = 'h';
// Displaying the values of the variables
Console.WriteLine("Simple Interest: " + si);
Console.WriteLine("Speed: " + s);
Console.WriteLine("Time: " + t);
Console.WriteLine("Character: " + var);
}
}
Output:
Simple Interest: 5.5
Speed: 20
Time: 10
Character: hNaming Rules of C# Variables
C# has certain rules that must be followed when naming variables.
1. Valid Characters
Variable names can contain letters, digits, and underscores (_). C# also supports Unicode characters in identifiers.
Valid examples:
int age;
int studentAge;
int total_marks;
2. Starting Character
A variable name cannot start with a digit. It generally starts with a letter or underscore.
Valid:
int age;
int _count;
Invalid:
int 2age;
3. No Reserved Keywords
C# reserved keywords such as int, class, and if cannot normally be used as variable names.
For example:
int int; // Invalid
If necessary, C# provides the @ prefix for using a keyword as an identifier:
int @class = 10;
However, using meaningful non-keyword names is generally preferred.
4. Case-Sensitive
C# is case-sensitive. Therefore, age, Age, and AGE are considered different identifiers.
int age = 20;
int Age = 30;
These are two different variables.
5. No Spaces
Variable names cannot contain spaces.
Invalid:
int student age;
Use camelCase instead:
int studentAge;
6. Use Meaningful Names
Variable names should clearly indicate what the variable stores.
Instead of:
int s = 90;
prefer:
int studentMarks = 90;
Meaningful names make the code easier to read and understand.
7. Naming Convention
For local variables and parameters, C# commonly uses camelCase.
Examples:
int studentAge;
double totalSalary;
string studentName;