Variables in C#

Notes:

Variables in C# Programming Language:

Variable:
Variable is a named memory location, whose value can change during the execution of a program.

Constant:
Constant is a named memory location, whose value never changes during the execution of a program once it is initialized.

i.e. constants stay constant whereas variables vary.

W.K.T. While developing any software application using C#; we will be dealing with different types of data and data values. In order to store and process different types of data & data values computer uses its memory (RAM). To allocate a chunk of memory and access it within a C# program, we need to declare a variable or a constant.

Declaring a variable or a constant:
- means allocating a memory location for some data

Initializing a variable or a constant:
- means assigning the initial value to that allocated memory location

Syntax for declaring a variable:
datatype nameOfVariable; // declaration of a single variable
Ex:
int playerScore;
int playerHealth;

Note: In C# default value of a variable or a constant depends upon the type of a variable or a constant

Note: default value of
- numeric type is 0
- bool data type is false
- char data type is '\0' character
- reference type is null

Note:
- Before using a local variable it has to be assigned or initialized with some meaningful value

Syntax for assigning value to a variable:
nameOfVariable = value; // changing value of a variable
Ex:
playerScore = 0;
playerHealth = 100;

Syntax for declaring and initializing a variable:
datatype nameOfVariable = ivalue; // declaration and initialization of a single variable
Ex:
int playerScore = 0;
int playerHealth = 100;

Syntax for declaring multiple variables:
datatype nameOfVariable1, nameOfVariable2,...; // declaration of multiple variables
Ex:
int playerScore, playerHealth;

Syntax for declaring & initializing multiple variables:
datatype nameOfVariable1 = ivalue, nameOfVariable2 = ivalue,…; // declaration & initialization of multiple variables
Ex:
int playerScore=0, playerHealth=100;

Example Code:

using System;

namespace VariablesDemo
{
class Program
{
static void Main(string[] args)
{
/*
int playerScore;
int playerHealth;

Console.WriteLine("player score = " + playerScore);
Console.WriteLine("player health = " + playerHealth);

int playerScore;
int playerHealth;

playerScore = 0;
playerHealth = 100;

Console.WriteLine("player score = " + playerScore); // 0
Console.WriteLine("player health = " + playerHealth); // 100

int playerScore = 0;
int playerHealth = 100;
Console.WriteLine("player score = " + playerScore); // 0
Console.WriteLine("player health = " + playerHealth); // 100

playerScore = 10;
playerHealth = 80;

Console.WriteLine("player score = " + playerScore); // 10
Console.WriteLine("player health = " + playerHealth); // 80

int playerScore, playerHealth;
Console.WriteLine("player score = " + playerScore);
Console.WriteLine("player health = " + playerHealth);
* * */

int playerScore = 0, playerHealth = 100;
Console.WriteLine("player score = " + playerScore); // 0
Console.WriteLine("player health = " + playerHealth);// 100

Console.ReadKey();
}
}
}