C# tutorials > Core C# Fundamentals > Basics and Syntax > How do you declare and initialize variables in C#?
How do you declare and initialize variables in C#?
Basic Declaration and Initialization
int, string, bool, double, char), then the variable name (e.g., age, name), and finally assign an initial value using the assignment operator (=). The data type must match the type of the value being assigned.
int age = 30;
string name = "John Doe";
bool isEmployed = true;
double salary = 50000.50;
char grade = 'A';
Declaration and Initialization on Separate Lines
int, the default value is 0. For booleans, it's false. For reference types like string, it's null. It is good practice to initialize variables as soon as possible.
int age;
age = 30;
string name;
name = "John Doe";
Understanding Data Types
Choosing the correct data type is crucial for efficient memory usage and preventing data type errors.int: Integer numbers (e.g., -10, 0, 30)string: Textual data (e.g., "Hello", "John")bool: Boolean values (true or false)double: Floating-point numbers (e.g., 3.14, 50000.50)char: Single characters (e.g., 'A', 'b')decimal: Used for financial calculations, offering higher precision than double (e.g., 1000.00m - note the 'm' suffix).
Implicitly Typed Variables (var)
var keyword allows the compiler to infer the data type of the variable based on the value assigned to it. However, the variable's type is still determined at compile time and cannot be changed later. Use var when the type is obvious from the initialization, but be mindful of readability. Overuse of var can sometimes make code harder to understand.
var age = 30;
var name = "John Doe";
Concepts Behind the Snippet
Real-Life Use Case Section
string), ages (int), grades (char), and GPAs (double or decimal). These variables would be used to display student information, calculate averages, and generate reports. Consider a game where you need to store the player's score (int), health (int), and name (string).
Best Practices
const keyword for variables whose values will not change during the program's execution (e.g., const double PI = 3.14159;). This improves performance and prevents accidental modification.readonly keyword allows you to assign a value to a variable only once, either during declaration or within the constructor of a class. This is useful for fields that should be initialized but not subsequently modified.
Interview Tip
int, bool, struct) and reference types (like string, class, array). Value types store their data directly in memory, while reference types store a reference (pointer) to the memory location where the data is stored. Understanding this distinction is crucial for comprehending how C# manages memory and how objects are passed around in your code. Be also ready to describe the difference between `const` and `readonly`.
When to Use Them
int for counting, string for text, and bool for representing true/false conditions.
Memory Footprint
int typically uses 4 bytes, bool uses 1 byte, double uses 8 bytes, and char uses 2 bytes. string is a reference type, so it uses memory to store the reference itself (typically 4 or 8 bytes, depending on the architecture) plus the memory required to store the string data itself, which depends on the length of the string. Using the right datatype will improve the memory usage of the program.
Alternatives
const for values that don't change.List, Dictionary, Array) to store multiple values of the same type.
Pros
Cons
FAQ
-
What happens if I don't initialize a variable?
If you declare a variable without initializing it, it will have a default value assigned to it depending on its data type. For example,intvariables will default to 0,boolvariables will default tofalse, and reference type variables (likestring) will default tonull. However, it's still considered good practice to explicitly initialize variables. -
Can I change the data type of a variable after it's been declared?
No, C# is a strongly-typed language, so you cannot change the data type of a variable after it has been declared. If you need to store data of a different type, you'll need to declare a new variable with the appropriate data type, or use type conversion if appropriate. -
What is the difference between `const` and `readonly`?
Both `const` and `readonly` are used for defining variables that cannot be changed after initialization, but they differ in when and how they are initialized. `const` variables must be initialized at compile time, and their values are embedded directly into the compiled code. `readonly` variables can be initialized either at the time of declaration or within the constructor of a class, allowing for more flexibility in initialization.