MSDN Documentation

Microsoft Developer Network

C# Variables

In C#, a variable is a container that holds a value. Variables are symbolic names for memory locations. They are essential for storing and manipulating data during program execution. Each variable has a specific data type, which determines the kind of data it can hold and the operations that can be performed on it.

Declaring Variables

To declare a variable in C#, you specify its data type followed by its name. You can optionally initialize the variable with a value at the time of declaration.


int age; // Declares an integer variable named 'age'
string name = "Alice"; // Declares a string variable named 'name' and initializes it
double price = 19.99; // Declares a double-precision floating-point variable
bool isActive = true; // Declares a boolean variable
        

Variable Naming Rules

Data Types

C# provides a rich set of built-in data types, categorized into value types and reference types.

Common Value Types:

Type Description Example Value
int 32-bit signed integer 100
long 64-bit signed integer 100000000000
float Single-precision floating-point 3.14f
double Double-precision floating-point 3.1415926535
decimal 128-bit high-precision floating-point (ideal for financial calculations) 19.99m
char 16-bit Unicode character 'A'
bool Boolean (true or false) true

Common Reference Types:


string greeting = "Hello, World!";
int[] numbers = { 1, 2, 3, 4, 5 };
        

Assigning Values

The assignment operator (=) is used to assign a value to a variable.


int count;
count = 50; // Assigns the value 50 to the 'count' variable

count = count + 10; // Assigns the result of the addition to 'count'
count += 10; // Shorthand for count = count + 10;
        

Implicitly Typed Local Variables (var)

C# 3.0 introduced implicitly typed local variables using the var keyword. When var is used, the compiler infers the data type of the variable from the initialization expression.

Note: var can only be used for local variables. It cannot be used for class fields or method parameters.

var message = "This is a message."; // Compiler infers 'string'
var quantity = 100; // Compiler infers 'int'
var pi = 3.14159; // Compiler infers 'double'

// The following is invalid:
// var invalidVar; 
        

Variable Scope

The scope of a variable determines where in the program that variable can be accessed. Variables declared within a block (e.g., inside curly braces {}) are local to that block.


void MyMethod()
{
    int localVar = 10; // localVar is accessible only within MyMethod

    if (localVar > 5)
    {
        string blockVar = "Inside if"; // blockVar is accessible only within this if block
        Console.WriteLine(blockVar);
    }
    // Console.WriteLine(blockVar); // Error: blockVar is out of scope here
}
        

Constants

Constants are variables whose values cannot be changed after they are assigned. They are declared using the const keyword and must be initialized at the time of declaration.


public const double PI = 3.14159;
const int MaxUsers = 100;
        

Constants are useful for representing fixed values that are used throughout the program, such as mathematical constants or configuration limits.