C#

C# Variable

What is Variable?

A variable is something you want the computer to remember while your program is running. Computer programs need places to store and process this information while working with it. They are called variables because the information stored there can change, or vary, during program execution. Variables are considered primary method for moving information around.

Note: Variables are case-sensitive.

Declaration and Instantiation using two statements

To declare a variable

string name;
int age;
int weight;
bool isMarried;

Run Demo

Variables in the same C# statement by specifying them in the form of a comma-separated “,” list if they are of the same type.

int age, weight;
// OR
int age,
    weight;

Use semi-colon separated “;” C# definition statements, if they are of different types.

int age, weight; string name; bool isMarried = true;
Definite Assignment
Console.WriteLine(name); //Error : Use of unassigned local variable 'name'

Run Demo

To initialize a variable

name = "Pirzada";
age = 35;
weight = 70;
isMarried = true;

Note: The term initialize means to assign an initial value.

Run Demo

age = true; //Error : Cannot implicitly convert type 'bool' to 'int'
age = "Pirzada";
// OR
age = name;

Above statement will give a compile-time error because string value cannot be assigned to an int type variable.

Run Demo

Declaration and Instantiation combined on a single line

You can combine the declaration and initialization statements at the same time on the same line, as shown below.

string name = "Pirzada";
int age = 35;
int weight = 70;
bool isMarried = true;

Run Demo

Mathematical expression
int wowExp = (3 + 2) * 4;

//Print Value
Console.WriteLine(wowExp); // 20

Run Demo

Use of variables in the mathematical expression.

int a, b, c;
a = 3;
b = 2;
c = 4;

int wowExp = (a + b) * c;

//Print Value
Console.WriteLine(wowExp); // 20

Run Demo

Assign same value to multiple different variables all at the same time:

int a, b, c;
a = b = c = 786;

//Print Values
Console.WriteLine(a); // 786
Console.WriteLine(b); // 786
Console.WriteLine(c); // 786

Run Demo

Using Type Inference

var keyword instructs the compiler to infer the type of the variable from the expression on the right side of the initialization statement. It just means that the compiler determines and assigns the most appropriate type.

var is optional.

Let’s use the same variables from above.

var name = "Pirzada";
var age = 35;
var weight = 70;
var isMarried = true;

Run Demo

Now the variables are implicitly typed local variables.

Suggestion: Using var is convenient, but use it only when the type is obvious from the right side of the assignment.

var income = 24899.45m;
var hourlyRate = 20.8f;

//Print Type
Console.WriteLine(income.GetType());
Console.WriteLine(hourlyRate.GetType());

//Print Values
Console.WriteLine(income);
Console.WriteLine(hourlyRate);

Note: var can only be declared and initialized in a single statement. Otherwise, the compiler doesn’t have anything from which to infer the type.

Run Demo

Default Value Expressions

default(T) expression: Old way
var name = default(string);
var age = default(int);
var weight = default(int);
var isMarried = default(bool);

// Print Values
Console.WriteLine(name); // null
Console.WriteLine(age); // 0
Console.WriteLine(weight); // 0
Console.WriteLine(isMarried); // False

Run Demo

default literal: New way
string name = default;
int age = default;
int weight = default;
bool isMarried = default;

// Print Values
Console.WriteLine(name); // null
Console.WriteLine(age); // 0
Console.WriteLine(weight); // 0
Console.WriteLine(isMarried); // False

Null value is printed as blank line.

Run Demo

OUTPUT for default literal & default(T)

 
0
0
False

new Operator

var age = new int(); // 0;
var isMarried = new bool(); // false;

Run Demo

OUTPUT

0
False

The following table shows the default value for the different predefined data types.

Type Default Value
sbyte, byte, short, ushort, int, uint, long, ulong 0
char ‘\0’
float 0.0f
double 0.0d
decimal 0.0m
bool False
object null
string null

Pirzada Rashid

Senior .NET developer with a Master of Science in Information Technology.
Teaching is one of my passions. It gives me great joy to share my knowledge with others, and I look forward to investing more energy into it.

Add Comment

Click here to post a comment

Most Popular

Popular Video

82,490+ Views