Variables
Variables in C#
A variable is a symbolic name given to a memory location. In C#, variables are categorized into two main types based on how they store data in memory: Value Types and Reference Types.
1. Value Types
Value types store the actual data directly in the location where the variable is declared (usually on the stack). When you copy a value type, a complete independent copy of the data is made.
int original = 100; int copy = original;
// A new copy is created copy = 200;
// Changing the copy does NOT affect the original
Console.WriteLine("Original: {original}, Copy: {copy}");
2. Reference Types
Reference types do not store the actual data. Instead, they store a reference (a pointer) to the memory address where the data is kept (usually on the heap). When you copy a reference type, you are only copying the pointer, not the data itself.
int[] originalArray = { 1, 2, 3 }; int[] aliasArray = originalArray;
// Both variables point to the same memory aliasArray[0] = 99;
// Changing aliasArray affects originalArray
Console.WriteLine("Original[0]: {originalArray[0]}, Alias[0]: {aliasArray[0]}");
Category |
Memory |
Typical Types |
Copy Behavior |
|---|---|---|---|
Value Types |
Stack |
int, double, bool, struct |
Copies the actual value |
Reference Types |
Heap |
string, class, Array, object |
Copies the reference (pointer) |
Variable Declaration and Initialization
C# is a strongly-typed language. You must declare the type of a variable before you use it. You can also use the var keyword for implicit typing, where the compiler determines the type based on the value assigned.
Examples
Example 1 : Explicit vs Implicit Typing
double precision = 0.0001; //Explicit
var tolerance = 1e-6; // Implicit (compiler sees it is a double)
Console.WriteLine($"Precision: {precision}, Tolerance: {tolerance}");
Ouput
Precision: 0.0001, Tolerance: 1E-06
Example 2 : Constant Variables
const double Gravity = 9.81;
// Gravity = 10;
// This would cause a compilation error
Console.WriteLine($"Acceleration due to gravity: {Gravity} m/s²");
Ouput
Acceleration due to gravity: 9.81 m/s²
Example 3 : Scope and Braces
int outer = 10;
{
int inner = 20;
Console.WriteLine($"Sum: {outer + inner}");
}
// Console.WriteLine(inner);
// Error: 'inner' is out of scope
Ouput
Sum: 30
Example 4 : Nullable Reference Types
string? name = null;
// The '?' allows the reference to be null
name = "Sepal Solver";
Console.WriteLine($"Project: {name}");
Ouput
Project: Sepal Solver