Lesson 03 of 30
Variables and Data Types
C# in Visual Studio 2026 — a hands-on guide for developers at every level.
Variables
A variable is a named storage location in memory. In C# you must declare a variable's type before using it. The compiler then checks that you never accidentally store the wrong kind of data in it.
int age = 25;
double price = 9.99;
string name = "Alice";
bool isActive = true;
Console.WriteLine(name + " is " + age + " years old.");
Built-in Value Types
| Type | Size | Range / Description | Example |
|---|---|---|---|
int | 32-bit | −2,147,483,648 to 2,147,483,647 | 42 |
long | 64-bit | Very large whole numbers | 9_000_000_000L |
double | 64-bit | Floating-point (15–17 sig. digits) | 3.14 |
decimal | 128-bit | High-precision (28 sig. digits) — use for money | 9.99m |
float | 32-bit | Single-precision float | 1.5f |
char | 16-bit | Single Unicode character | 'A' |
bool | 1-bit | true or false | true |
The var Keyword
C# can infer the type from the right-hand side using var. The variable is still strongly typed — the compiler just figures out the type for you:
var score = 100; // inferred as int
var pi = 3.14159; // inferred as double
var label = "Hello"; // inferred as string
Constants
Use const to declare a value that must never change. The compiler will flag any attempt to reassign it:
const double Pi = 3.14159265358979;
const int MaxRetries = 3;
Nullable Types
Value types cannot normally be null. Append ? to make them nullable:
int? optionalAge = null;
if (optionalAge is not null)
Console.WriteLine($"Age: {optionalAge}");