This article will explain the different types of variables available in C#. The three major types of variables are given below.
- Class variables
- Function level variables
- Block variables
Class variables
Class level variables are called instance variables. It will be declared in the class itself with any of the access specifiers and we can use it anywhere in the class. If we declare a variable with public access modifier it can be accessed outside. If a variable is protected, derived class has access to use it.
This type of variable will be initialized once the object is created. It takes a default value like integer and float 0, boolean false. Class variables can also be declared as static, this can be used in the static as well as non-static methods. The same is explained in the below code.
Example
namespace ConsoleExamples {
class Program {
int instanceMember = 10; //instance field
static float staticMember; //static field
protected int intDerived; //instance class variable
public void Voidmethod() //non static function
{
Console.WriteLine(instanceMember);
Console.WriteLine(staticMember);
}
public static void VoidstaticMethod() //static function
{
Console.WriteLine(staticMember);
//Console.WriteLine(instanceMember);
}
static void Main(string[] args) {
VoidstaticMethod();
Program obj = new Program();
ProgramDerived objDerived = new ProgramDerived();
obj.Voidmethod();
objDerived.VoidmethodDerived();
}
}
class ProgramDerived: Program {
public void VoidmethodDerived() {
Console.WriteLine(intDerived);
}
}
}
Function variables
Function variables are called local variables. It has access only inside a method alone. Local variables can't be used outside.
While function scope is ended inside a function variable is also lost. The same name for two variables should not be used within a function. Without assigning a value can't have access to use local variables. Function variables are not taken as default values. A static function variable is not valid in a non-static function. Local variables can't become static or dynamic.
Example
class Program
{
int instanceMember = 10; //instance field
static float staticMember; //static field
protected int intDerived; //instance class variable
public void Voidmethod() //non static function
{
int localvarible = 12; // Local variables
//static int 120; //static function variable not valid in non static function
Console.WriteLine(localvarible);
Console.WriteLine(instanceMember);
Console.WriteLine(staticMember);
}
public static void VoidstaticMethod() //static function
{
// dynamic float floatvar =10.0f; //not vaild
int localvarible = 1;
Console.WriteLine(localvarible);
Console.WriteLine(staticMember);
//Console.WriteLine(instanceMember);
}
static void Main(string[] args)
{
VoidstaticMethod();
Program obj = new Program();
ProgramDerived objDerived = new ProgramDerived();
obj.Voidmethod();
}
}
class ProgramDerived : Program
{
public void VoidmethodDerived()
{
Console.WriteLine(intDerived);
}
}
Block variable
Block variable is mostly used in looping statements. Before the block is declared variables can have access inside the loop and methods. Once block execution is finished block variables did not exist. In nested looping statements have access to method variable, class variable, and block variable. Block variables are also termed loop variables or looping statements.
Example
class Program {
int instanceMember = 10; //instance field
static float staticMember = 5; //static field
protected int intDerived; //instance class variable
static void Main(string[] args) {
int i, j;
Program obj = new Program();
for (i = 0; i < staticMember; i++) {
Console.WriteLine("*");
for (j = 0; j == i; j++) {
Console.WriteLine("#");
}
}
}
}
This article will help you to understand the variable scopes.