Types Of Variable in C#
A variable in C# is a given name to
a storage space in a program as well as a given name to a data value. Each variable
has its type and size w.r.t a value
We have the following types of
variables in C#.
Local Variable:
A variable declared within a block /
method / constructor is called as Local Variable because it’s
local to that particular block in a program.
A variable got created when the
execution starts or enter in that block and getting destroyed once the
execution is over.
As it’s local to a block its scope
will be within that block.
using System;
namespace
CSharp.Tutorial
{
public class Program
{
public string
DisplayName()
{
string name = "SKMDotNet";
return name; // name is
local to this block/method only
}
static void Main(string[] args)
{
// Types of variable in C#
Program objProgram = new Program();
Console.WriteLine(objProgram.DisplayName());
Console.ReadLine();
}
}
}
Instance
or Non-Static Variable:
Instance or Non-Static
variables declared in a class and outside of a block / method /
constructor.
As these variables declared inside
a class so it got created when the object of that class got created and
destroys once the object gets disposed.
To access these variables, we
need to create object of the class it has and using that it can be accessed.
Each variable creates it's own copy according to objects.
using System;
namespace
CSharp.Tutorial
{
public class Program
{
string name; //
Instance/Non-Static
static void Main(string[] args)
{
// Types of variable in C#
Program objProgram1 = new Program();
objProgram1.name = "SKMDotNet";
Program objProgram2 = new Program();
objProgram2.name = "SKMDotNet
Education";
Console.WriteLine(objProgram1.name);
Console.WriteLine(objProgram2.name);
Console.ReadLine();
}
}
}
Static Variable:
These variables also terms as class level
variable. If a variable declared by static modifier or with in a
static block then its called as static variable.
Static variable got created when the program execution starts and
ends up with the execution wind up.
Differences between
Instance & Static
·
Instance
variable will have its own copy whereas
only one copy will be in case of static variable irrespective of how
many objects one class has.
· Changes made at one Instance variable won’t
reflect on another objects as each persists its own copy but in case of static
variable it will reflect on another.
· Instance variable can be accessed using object of the class but in case
of static variable these can accessible directly using class name with
‘.’ operator.
· Instance variable gets instantiated every time the objects gets created
whereas static variable initialized only one time in life cycle.
using System;
namespace
CSharp.Tutorial
{
public class Program
{
static string name; // Static
Variable
static void Main(string[] args)
{
// Types of variable in C#
name = "SKMDotNet"; // Accessing
directly
Console.WriteLine(name);
Program.name = "SKMDotNet
Education";
Console.WriteLine(name);
Console.ReadLine();
}
}
}
Constant Variable:
The
variable declared using “const” keyword called as Constant Variable.
Also, it can’t be modified once declared so it must be assigned at time of declaration.
The
behavior of static and constant variable are same i.e. one time
initialization in life cycle and doesn’t require object of class to accessing
or initializing.
The
difference between static and constant is static variable can be
modified once declared but constant variable can’t.
using System;
namespace
CSharp.Tutorial
{
public class Program
{
const string title="Mr."; // Constant Variable
static string name; // Static
Variable
static void Main(string[] args)
{
// Types of variable in C#
Program.name="Ram";
Console.WriteLine($"{title} {Program.name}");
Console.ReadLine();
}
}
}
Readonly Variable:
The variable declared with “readonly” keyword
called as Readonly Variable. Also, it can’t be modified as constant but
after initialization.
The
behavior of readonly and instance variable are same i.e. initialized
only after creating object of the class.
The
difference between readonly and instance is instance variable can be
modified but
readonly variable can’t.
Constant
variable has a fixed value for whole class where as readonly has a fix value
specific to object of class.
using System;
namespace
CSharp.Tutorial
{
public class Program
{
const string title = "Mr."; // Constant Variable
readonly string name; // Static
Variable
public Program(string value)
{
Console.WriteLine("Inside
Constructor: " + title);
name = value;
}
static void Main(string[] args)
{
// Types of variable in C#
Program objProgram1 = new Program("Sunil");
Console.WriteLine($"{objProgram1.name}");
Program objProgram2 = new Program("Sunil2");
Console.WriteLine($"{objProgram2.name}");
Console.WriteLine(title);
Console.ReadLine();
}
}
}
No comments:
Post a Comment