C# Variables
Learn how to store and manage data in C#
📦 What are Variables?
Variables are containers for storing data values in C#. They hold information that your program can use and modify during execution, making your code dynamic and flexible.
// This is a simple variable example
string name = "John";
int age = 25;
Console.WriteLine(name); // Output: John
Console.WriteLine(age); // Output: 25
Output:
John
25
Key Variable Concepts
Declaration
Creating a variable with a type
int number;
string text;
Initialization
Assigning a value to a variable
int number = 10;
string text = "Hello";
Reassignment
Changing the value of a variable
int x = 5;
x = 10; // New value
Constants
Variables that cannot be changed
const double PI = 3.14159;
🔹 Declaring Variables
In C#, variables are declared by specifying a data type followed by a meaningful name. You can initialize them immediately or separately for clarity. Proper variable declaration—such as int age = 30; or string name = "Alice";—lays the foundation for organized, maintainable code and efficient memory usage in any application.
// Declare and initialize
int age = 30;
string name = "Alice";
double price = 19.99;
bool isActive = true;
// Declare first, initialize later
int count;
count = 100;
Console.WriteLine(age); // Output: 30
Console.WriteLine(name); // Output: Alice
Console.WriteLine(price); // Output: 19.99
Console.WriteLine(isActive); // Output: True
Console.WriteLine(count); // Output: 100
Output:
30
Alice
19.99
True
100
🔹 Variable Naming Rules
Follow C# naming conventions to write clean, readable, and error-free code. Variable names must begin with a letter or underscore, can include letters, digits, and underscores, and are case-sensitive. Adhering to rules—like using camelCase for local variables—improves code clarity, collaboration, and long-term maintainability in development projects.
// Valid variable names
int age;
string firstName;
double _price;
int number123;
// Invalid variable names (will cause errors)
// int 123number; // Cannot start with a number
// string first-name; // Cannot use hyphens
// double my price; // Cannot have spaces
// Case-sensitive examples
int myVariable = 10;
int MyVariable = 20;
Console.WriteLine(myVariable); // Output: 10
Console.WriteLine(MyVariable); // Output: 20
Output:
10
20
🔹 Multiple Variables
Declare multiple variables of the same type in a single line to write concise and efficient C# code. Using comma-separated lists—like int x = 5, y = 10, z = 15;—reduces redundancy, improves readability, and is especially useful when initializing related values such as coordinates, scores, or configuration settings in your program.
// Declare multiple variables in one line
int x = 5, y = 10, z = 15;
Console.WriteLine(x); // Output: 5
Console.WriteLine(y); // Output: 10
Console.WriteLine(z); // Output: 15
// You can also assign the same value
int a, b, c;
a = b = c = 50;
Console.WriteLine(a); // Output: 50
Console.WriteLine(b); // Output: 50
Console.WriteLine(c); // Output: 50
Output:
5
10
15
50
50
50
🔹 Constants
Use the const keyword to define immutable values that remain fixed throughout program execution. Constants like const double Pi = 3.14159; ensure data integrity, prevent accidental changes, and improve code readability. They are ideal for mathematical values, configuration settings, or application-wide identifiers that should never be modified.
// Declare a constant
const double PI = 3.14159;
const int MAX_USERS = 100;
const string APP_NAME = "MyApp";
Console.WriteLine(PI); // Output: 3.14159
Console.WriteLine(MAX_USERS); // Output: 100
Console.WriteLine(APP_NAME); // Output: MyApp
// This would cause an error:
// PI = 3.14; // Cannot modify a constant
Output:
3.14159
100
MyApp
🔹 Variable Scope
Understand variable scope in C# to manage where variables are accessible and avoid common errors. Local variables declared inside a method are only accessible within that method, while class-level variables have broader access. Proper scope management prevents naming conflicts, reduces bugs, and promotes cleaner, more modular code design in complex applications.
class Program
{
// Class-level variable (accessible everywhere in the class)
static int globalVar = 100;
static void Main()
{
// Local variable (only accessible in Main method)
int localVar = 50;
Console.WriteLine(globalVar); // Output: 100
Console.WriteLine(localVar); // Output: 50
DisplayMessage();
}
static void DisplayMessage()
{
Console.WriteLine(globalVar); // Output: 100
// Console.WriteLine(localVar); // Error: localVar not accessible here
}
}
Output:
100
50
100