C# Variables
In this chapter you will learn:
- What are variables
- Syntax to define C# variables
- Example for C# variables
- C# program for variable definition
Description
Variables are used to store values.
Syntax
You declare variables in C# using the following syntax:
datatype identifier;
Example
For example:
int i;
This statement declares an int
named i
.
After it has been declared, you can assign a value to the variable
using the assignment operator, =
:
i = 10;
You can also declare the variable and initialize its value at the same time:
int i = 10;
If you declare and initialize more than one variable in a single statement, all of the variables will be of the same data type:
int x = 10, y =20; // x and y are both ints
Example 2
Let's create another C# program.
class Program/*from w w w.j a v a2 s. c o m*/
{
static void Main(string[] args)
{
int i = 0;
int j = 2;
System.Console.WriteLine("i=" + i);
System.Console.WriteLine("j=" + j);
}
}
The code above generates the following result.
Next chapter...
What you will learn in the next chapter:
- What are C# comments
- What are the three types of comments
- Single line comments
- Multiple line comments
- XML documentation comments