Variable (C#)

A variable in C# is a named storage location that holds a value of a specific type. You declare it by writing the type followed by a name, then assign a value with the equals sign.

C#

C# is statically typed, so every variable has a fixed type known at compile time. Once a variable is an int, it can only hold integers.

You can let the compiler figure out the type for you with the var keyword, but the variable is still strongly typed under the hood.

Syntax

type name = value;

Examples

C#

int age = 25;
string name = "Maya";
age = age + 1;
Console.WriteLine($"{name} {age}");

Output:

Maya 26

Learn it in a lesson

See also

Back to the Dictionary