How to qualify various component in C# with static keyword.
Understand Static in
C#
In this article we will see various uses of static in C#.
static qualifier can able to use with various components of C# language. For
example we can qualify a class as a static class, a function as a static function,
a data member as a static data member etc. Let’s try to understand each one of them.
Static Class:-
We can qualify a class with static keyword. In below there
is one example of static keyword.
static class StaticTest
{
public static int member;
public static void StaticFunction()
{
}
}
Property:-
If we declare a class as static then all the member of this
class needs to declare as static. Static class contains only static member.
We cannot instantiate a static class. If we want to access
any member of static class then we have to access by dot operator associated
with class name.
Static function:-
Like class we can qualify a function as static function. If we define a static function then the
function will be accessible by class name. Have a look on below code.
using System;
using System.Collections.Generic;
using System.Text;
namespace Client
{
class StaticTest
{
public static void StaticFunction()
{
Console.WriteLine("Static Function");
}
}
class Program
{
static void Main(string[] args)
{
StaticTest.StaticFunction();
Console.ReadLine();
}
}
}
Here is sample output.
If we observe closely the body of Main()
function ,we will find that we are using class name to call the function.
Property:-
The static function can able to process
only static data. So if we want to work with any data within static function
then the data have to be static.
The reason is, if we define a function as
static it does not belongs to any particular object.
Static data member:-
We can qualify a data member as static
ember. If we declare a member as static then it will not belong to any
particular object. It will treat as common property of that class and any
object can able to use it. Let’s see below example.
using System;
using System.Collections.Generic;
using System.Text;
namespace Client
{
class StaticTest
{
public static int value = 0;
public void Increment()
{
value++;
}
public void ShowValue()
{
Console.WriteLine("Value is :- " + value);
}
}
class Program
{
static void Main(string[] args)
{
StaticTest a = new StaticTest();
a.Increment();
StaticTest b = new StaticTest();
b.ShowValue();
Console.ReadLine();
}
}
}
output of this code
Here we have created two objects within
Main() function and by first object we are modifying value of static data
member then we are accessing it using another object. We are seeing that the modified
value is getting reflect in all (second) object.
Property:-
Static data member does not belong to
particular object. It’s a common property of the class , any object can able to
use and modify it.
Static property:-
Like class , function and member we can
declare static property. It will behave very similar with static function. We
can use class name to access static property. Have a look on below code.
using System;
using System.Collections.Generic;
using System.Text;
namespace Client
{
class StaticTest
{
public int value;
public static int getSetvalue
{
get;
set;
}
}
class Program
{
static void Main(string[] args)
{
StaticTest.getSetvalue = 100;
Console.WriteLine("Value is :- " + StaticTest.getSetvalue);
Console.ReadLine();
}
}
}
Output.
Conclusion:-
In this article we have seen how to
qualify various components with static keyword in C#. Hope you have understood
the concept.