Variable
C# Variable
A variable is a name of memory location. It is used to store data. Its value can be changed and it can be reused many times. It is a way to represent memory location through symbol so that it can be easily identified. The basic variable type available in C# can be categorized as:
- int - stores integers (whole numbers), without decimals, such as 123 or -123
- double - stores floating point numbers, with decimals, such as 19.99 or -19.99
- char - stores single characters, such as 'a' or 'B'. Char values are surrounded by single quotes
- string - stores text, such as "Hello World". String values are surrounded by double quotes
- bool - stores values with two states: true or false
Declaring (Creating) Variables
Syntax for variable definition in C# is -
<data_type> <variable_list>;
Here, data_type must be a valid C# data type including char, int, float, double, or any user-defined data type, and variable_list may consist of one or more identifier names separated by commas.
Some valid variable definitions are shown here -
int i, j, k;
char c, ch;
float f, marks;
double d;
You can initialize a variable at the time of definition as -
int i = 100;
Above, int is a data type, i is a variable name (identifier). The = operator is used to assign a value to a variable. The right side of the = operator is a value that will be assigned to left side variable. Above, 100 is assigned to a variable i.
Initializing Variables
Variables are initialized (assigned a value) with an equal sign followed by a constant expression. The general form of initialization is -
variable_name = value;
Variables can be initialized in their declaration. The initializer consists of an equal sign followed by a constant expression as −
<data_type> <variable_name> = value;
A demonstration of how to declare variables of other types:
int myNum = 5;
double
myDoubleNum = 5.99D;
char myLetter = 'D';
bool myBool = true;
string myText = "Hello";
Rules for defining variables
- A variable can have alphabets, digits and underscore.
- A variable name can start with alphabet and underscore only. It can't start with digit.
- No white space is allowed within variable name.
- A variable name must not be any reserved word or keyword e.g. char, float etc.
Example
using System;
public class MyApplication
{
public static void Main()
{
int i = 200;
int j = i + 20;
Console.WriteLine("j = {0}", j);
i = 300;
j = i + 20;
Console.WriteLine("j = {0}", j);
i = 400;
Console.WriteLine("j = {0}", j);
}
}
OUTPUT
j = 220
j = 320
j = 320
In the above example, value of j depends on the value of i. You must re-execute expression each time you change the value of i; otherwise, value of j would not change based on the value of i.
Valid variable names:
int y;
int _y;
int x20;
Invalid variable names:
int 6;
int a b;
int float;
Multiple Variables in a Single Line
using System;
public class MyApplication
{
public static void Main()
{
int i = 0, j = 10, k = 200;
Console.WriteLine(j);
}
}
OUTPUT
10
C# Identifiers
All C# variables must be identified with unique names. These unique names are called identifiers. Identifiers can be short names (like x and y) or more descriptive names (age, sum, totalVolume). Note: It is recommended to use descriptive names in order to create understandable and maintainable code:
using System;
namespace MyApplication
{
class Program
{
static void Main(string[] args)
{
// Good
int age = 30;
// OK, but not so easy to understand what a actually is
int a = 30;
Console.WriteLine(age);
Console.WriteLine(a);
}
}
}
OUTPUT
30
30
Note: C# is the strongly typed language. It means you can assign a value of the specified data type. You cannot assign an integer value to string type or vice-versa.
There are few example which create error by this declaration of variable
int num = "Hello"; // Cannot assign string to int type variable
int num;
num = 200; //Variables can be declared first and initialized later.
int i;
int j = i; //compile-time error: Use of unassigned local variable 'i'