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.
csharp1int age = 25; // A variable named 'age' storing the number 252string name = "Alice"; // A variable named 'name' storing text "Alice"
Variable Declaration Syntax
In C#, you declare a variable by specifying:
- The data type (what kind of data it holds)
- The variable name (identifier)
- Optionally, an initial value
csharp1// Declaration with initialization2int score = 100;34// Declaration without initialization5int count;6count = 50; // Assign value later78// Multiple variables of the same type9int 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 (
ageandAgeare different variables)
Examples
csharp1// Valid names2int age;3string firstName;4double _temperature;5int player1Score;6string userName123;78// Invalid names9// int 123abc; // Cannot start with a number10// string my-name; // Cannot contain hyphens11// int class; // Cannot use reserved keywords
Naming Conventions
While not required, following conventions makes code readable:
camelCase (for local variables and parameters)
csharp1int playerScore;2string firstName;3double accountBalance;
PascalCase (for public members, classes, methods)
csharp1// We'll cover these when we learn about classes2public string FirstName;3public void CalculateTotal();
The var Keyword
C# can infer the type from the assigned value using var:
csharp1var age = 25; // Compiler knows this is int2var name = "Alice"; // Compiler knows this is string3var price = 19.99; // Compiler knows this is double4var isActive = true; // Compiler knows this is bool
When to Use var
csharp1// Good: Type is obvious from the right side2var message = "Hello";3var numbers = new List<int>();45// Less clear: Type not obvious6var result = CalculateSomething(); // What type is result?78// Cannot use var without initialization9// var x; // Error! Compiler cannot infer type
var vs Explicit Types
Both are valid approaches:
csharp1// Explicit type (traditional)2string greeting = "Hello";3int count = 42;45// 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:
csharp1const double PI = 3.14159;2const int MaxPlayers = 4;3const string AppName = "MyApp";45// Constants must be initialized at declaration6// const int x; // Error!78// Constants cannot be changed9// PI = 3.14; // Error! Cannot modify a constant
Default Values
Uninitialized variables in certain contexts have default values:
| Type | Default Value |
|---|---|
| int | 0 |
| double | 0.0 |
| bool | false |
| char | '\0' |
| string | null |
However, local variables must be assigned before use:
csharp1int x;2// Console.WriteLine(x); // Error! Use of unassigned variable34x = 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
varfor type inference when the type is obvious - Use
constfor values that never change
Next, we'll explore the different data types available in C#.