10 minlesson

Declaring Variables

Declaring Variables

Variables are containers that store data in your program. In this lesson, you'll learn how to declare and use variables in C#.

What is a Variable?

A variable is a named storage location in your computer's memory. Think of it as a labeled box where you can store a value and retrieve it later.

csharp
1int age = 25; // A variable named 'age' storing the number 25
2string name = "Alice"; // A variable named 'name' storing text "Alice"

Variable Declaration Syntax

In C#, you declare a variable by specifying:

  1. The data type (what kind of data it holds)
  2. The variable name (identifier)
  3. Optionally, an initial value
csharp
1// Declaration with initialization
2int score = 100;
3
4// Declaration without initialization
5int count;
6count = 50; // Assign value later
7
8// Multiple variables of the same type
9int x = 10, y = 20, z = 30;

Variable Naming Rules

C# has specific rules for variable names:

Must Follow

  • Start with a letter or underscore (_)
  • Contain only letters, numbers, and underscores
  • Cannot be a C# keyword (like int, class, if)
  • Case-sensitive (age and Age are different variables)

Examples

csharp
1// Valid names
2int age;
3string firstName;
4double _temperature;
5int player1Score;
6string userName123;
7
8// Invalid names
9// int 123abc; // Cannot start with a number
10// string my-name; // Cannot contain hyphens
11// int class; // Cannot use reserved keywords

Naming Conventions

While not required, following conventions makes code readable:

camelCase (for local variables and parameters)

csharp
1int playerScore;
2string firstName;
3double accountBalance;

PascalCase (for public members, classes, methods)

csharp
1// We'll cover these when we learn about classes
2public string FirstName;
3public void CalculateTotal();

The var Keyword

C# can infer the type from the assigned value using var:

csharp
1var age = 25; // Compiler knows this is int
2var name = "Alice"; // Compiler knows this is string
3var price = 19.99; // Compiler knows this is double
4var isActive = true; // Compiler knows this is bool

When to Use var

csharp
1// Good: Type is obvious from the right side
2var message = "Hello";
3var numbers = new List<int>();
4
5// Less clear: Type not obvious
6var result = CalculateSomething(); // What type is result?
7
8// Cannot use var without initialization
9// var x; // Error! Compiler cannot infer type

var vs Explicit Types

Both are valid approaches:

csharp
1// Explicit type (traditional)
2string greeting = "Hello";
3int count = 42;
4
5// var keyword (type inference)
6var greeting = "Hello";
7var count = 42;

Choose based on readability. If the type isn't obvious, use explicit types.

Constants

Use const for values that never change:

csharp
1const double PI = 3.14159;
2const int MaxPlayers = 4;
3const string AppName = "MyApp";
4
5// Constants must be initialized at declaration
6// const int x; // Error!
7
8// Constants cannot be changed
9// PI = 3.14; // Error! Cannot modify a constant

Default Values

Uninitialized variables in certain contexts have default values:

TypeDefault Value
int0
double0.0
boolfalse
char'\0'
stringnull

However, local variables must be assigned before use:

csharp
1int x;
2// Console.WriteLine(x); // Error! Use of unassigned variable
3
4x = 10;
5Console.WriteLine(x); // OK: x is now assigned

Summary

In this lesson, you learned:

  • Variables are named storage locations for data
  • Declare variables with a type, name, and optional initial value
  • Follow naming rules and conventions (camelCase for local variables)
  • Use var for type inference when the type is obvious
  • Use const for values that never change

Next, we'll explore the different data types available in C#.