C# VS2026
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

TypeSizeRange / DescriptionExample
int32-bit−2,147,483,648 to 2,147,483,64742
long64-bitVery large whole numbers9_000_000_000L
double64-bitFloating-point (15–17 sig. digits)3.14
decimal128-bitHigh-precision (28 sig. digits) — use for money9.99m
float32-bitSingle-precision float1.5f
char16-bitSingle Unicode character'A'
bool1-bittrue or falsetrue

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}");