Variable Declaration in C#
From the C# Programming Language cheat sheet · Variables & Data Types · verified Jul 2026
Variable Declaration
Declaring and initializing variables
csharp
// Declaration and initialization
int age = 25;
string name = "John";
// Declaration then assignment
int score;
score = 100;
// Multiple declarations
int x = 1, y = 2, z = 3;
// Type inference with var
var message = "Hello"; // Compiler infers string
var number = 42; // Compiler infers int
var price = 19.99; // Compiler infers double
// Constants (cannot change)
const double PI = 3.14159;
const int MAX_SIZE = 100;💡 Use var when type is obvious from right side
âš¡ const for compile-time constants, readonly for runtime
📌 Variables are case-sensitive (age ≠Age)
🟢 Follow C# naming conventions: camelCase for variables