C# Variables
- Variables stand for a small part of memory you will use in the code.
- The length of the memory you are using depends on data type and its length.
- C# variables are type restricted so you will have to define each of the data type of the variable before you use it.
- Variables can be assign an initial value at the declaration statement.
- After a variable is declared, you can assign a value to it or get it out in the code.
The syntax to define variables is shown below.
<Data Type> <Variable List|Variable = Initial Value[,]>
Example 01-03-01
1
2
3
4
5
2
3
4
5
int i, j=0, k; double xx; char ch1 = 'X', ch2='Y'; string s1 = "123abc"; object ob = new Object();
- Line 1: Declare 3 integer type variables i, j and k with j has initial 0 value.
- Line 2: Declare a double type variable.
- Line 3: Declare 2 char variables with initial values.
- Line 4: Declare a string variable s1 with its initial value.
- Line 5: Declare an Object variable ob and instantiate an instance to it.
![]() |
---|
A variable cannot be declared twice. |
- Variable names cannot be defined as keyword. For example, "class", "namespace" or "int". Each keyword has its specific meaning and translated into code by C# compiler.
- Variable names must start with a letter, an underscore or @ symbol.
- The subsequent characters can be letters, underscores or numbers.
The following are the good examples of the variable names.
p45ab @de123 _123ab
And here is the bad examples.
1
2
3
2
3
45xyz using It-is-not-right
The reasons are as follows.
- Line 1: The variable name starts with a digit.
- Line 2: The variable name is using a keyword.
- Line 3: The variable name contains minus character(-) which is not allowed.
"=" is the assignment operator. It is also used in declaration statement when a variable is initialized. See the syntax below.
<Variable Name> = <Value of Data Type>;
Example 01-03-02
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
using System; namespace TestVariables { class Program { static void Main(string[] args) { int i, j = 1, k; i = 2; Console.WriteLine("i={0}, j={1}", i, j); k = i; i = j; j = k; Console.WriteLine("i={0}, j={1}", i, j); Console.Read(); } } }
Output
i=2, j=1 i=1, j=2
- Line 9: Declare 3 integer type variables i, j and k and j is initialized as 1 value. It equals to the following code.
- Line 10: Assign 2 to i.
- Line 11: Output variable i and j. {0} is place holder and will be replaced with the second parameter i value. {1} is replaced with j value. So the first line of the output is what we expect.
- Line 12: We use this statement to assign i's value to k. So k's is the same as i's after Line 12.
- Line 13: Assign j's value to i.
- Line 14: Assign k's value to j. so Line 12-14 makes i's value and j's value exchanged by the bridge variable k.
- Line 15: Output i and j's value. It is the same as line 11. Check the second output above they were exchanged.
int i, j, k;
j=1;
What happened if Line 10 is removed ?
You will get compile error. Because no value is assigned to i before it is outputted.
![]() |
---|
Variables must be assigned before they are used. |