Lesson: C# Variables

What are Variables in C#?

In C#, a variable is a storage location in memory that has a name and is used to hold data values. Variables are essential in programming because they allow you to store, modify, and retrieve data. In C#, variables are strongly typed, meaning you need to declare the type of data a variable will hold.

Declaring Variables

To declare a variable in C#, you specify the type followed by the variable name. For example:

int age = 25;

This declares a variable named "age" of type int (integer), with an initial value of 25.

Common Variable Types in C#

Assigning Values

After declaring a variable, you can assign a new value to it using the assignment operator (=). For example:

age = 30;

This updates the value of "age" to 30.

Interactive Code Example

Change the value of the variable and see the result:

Age: 25

Quiz: Test Your Knowledge

1. Which of the following is the correct way to declare a variable in C#?




2. What is the purpose of a variable in programming?




3. Which type of variable can store a decimal number?




4. What type of data can a string variable hold?