In programming, a variable is a storage location paired with an associated symbolic name (an identifier), which contains some known or unknown quantity of information referred to as a value. In C#, variables are used to store data that can be manipulated and accessed throughout the execution of a program.
Variables allow you to write flexible and reusable code by storing values that can change during runtime. Understanding how to declare, initialize, and use variables is fundamental to programming in C#.
In C#, every variable must be declared before it can be used. Declaration involves specifying the data type of the variable and giving it a name. The syntax for declaring a variable is as follows:
<DataType> <VariableName>;
For example, to declare an integer variable named age, you would write:
int age;
Once declared, a variable can be assigned a value using the assignment operator (=). For instance:
age = 25;
You can also declare and initialize a variable in a single line:
int age = 25;
Let's look at some practical examples to illustrate how variables work in C#.
In this example, we'll declare several variables of different data types and initialize them with values.
using System;
class Program
{
static void Main()
{
// Declare and initialize an integer variable
int age = 25;
// Declare and initialize a string variable
string name = "John Doe";
// Declare and initialize a double variable
double height = 5.9;
// Display the values of the variables
Console.WriteLine("Name: " + name);
Console.WriteLine("Age: " + age);
Console.WriteLine("Height: " + height);
}
}
When you run this program, the output will be:
<OutputBlock>
{`Name: John Doe
Age: 25
Height: 5.9`}
</OutputBlock>
### Example 2: Variable Scope
Variables in C# have a scope that determines where they can be accessed within the code. The most common scopes are local and global.
- **Local Variables**: These are declared inside a method or a block of code and can only be accessed within that method or block.
- **Global Variables**: These are declared outside of any method but within a class and can be accessed by all methods in that class.
Here's an example demonstrating variable scope:
```csharp
using System;
class Program
{
// Global variable
static int globalCounter = 0;
static void Main()
{
// Local variable
int localCounter = 10;
Console.WriteLine("Global Counter: " + globalCounter);
Console.WriteLine("Local Counter: " + localCounter);
}
}
When you run this program, the output will be:
<OutputBlock>
{`Global Counter: 0
Local Counter: 10`}
</OutputBlock>
### Example 3: Variable Types
C# supports various data types for variables, including integers (`int`), floating-point numbers (`double`), characters (`char`), and strings (`string`). Here's an example using different variable types:
```csharp
using System;
class Program
{
static void Main()
{
// Integer variable
int number = 10;
// Double variable
double pi = 3.14159;
// Character variable
char initial = 'J';
// String variable
string message = "Hello, World!";
Console.WriteLine("Number: " + number);
Console.WriteLine("Pi: " + pi);
Console.WriteLine("Initial: " + initial);
Console.WriteLine("Message: " + message);
}
}
When you run this program, the output will be:
<OutputBlock>
{`Number: 10
Pi: 3.14159
Initial: J
Message: Hello, World!`}
</OutputBlock>
## What's Next?
In the next section, we'll explore different data types available in C# and how they can be used to store various kinds of information. Understanding data types is crucial for writing efficient and effective C# programs.
Stay tuned!