Variables in C#: Types of Variables with Examples
Understand what variables are in C#, the different types of variables based on scope and data type, and see clear examples of each.
What is a Variable?
A variable in C# is a named storage location in memory used to hold a value that can change during program execution. Every variable has a data type that determines what kind of value it can store.
Value Type Variables
Value type variables, such as int, float, bool, and struct, store their data directly in memory and are copied by value whenever assigned to another variable or passed to a method.
Reference Type Variables
Reference type variables, such as classes, arrays, and strings, store a reference (memory address) to the actual data, meaning multiple variables can point to and modify the same underlying object.
Local, Instance, and Static Variables
Beyond data type, variables can be categorized by where they're declared: local variables exist inside methods, instance variables belong to individual objects, and static variables are shared across all instances of a class.
.png)