As we know static means it is associated with specific type instead of instance of type. Today we discuss about when we have to use static keyword, what are the static variable, static method and static class in C#.
We can declare any variable or method which don’t requires instance of the class to access. That means if you declare the variable as static then you have to access that variable by using its class name itself instead of instance of the class.
Static Variables:
We can declare the static variables by using static keyword. We have to declare the any variable as static when the value of the particular variable is not requires instance of the class to access. That means declare variable as static who’s is value is same for all request for the class. For example if we are declaring PI variable, we can declare it as static because its value is same for all users.
Declare the static variables as shown below.
class Circle
{
public static double PI = 3.14159;
}
class Program
{
static void Main(string[] args)
{
Console.WriteLine(Circle.PI.ToString());
Console.ReadLine();
}
}
As shown above we declare the PI variable with static keyword and we are accessing this static variable with the help of class name instead of instance of class. Static variables don’t requires instance of the class because memory allocated to static variables at the time of compile-time only.
If you trying to access the above static variables through instance of class, we will get compile-time error as “Circle.PI cannot be accessed with an instance reference; qualify it with a type name instead”
Static Methods:
We can declare any method as Static method by using static keyword. Static method can access only static members, it cannot access instance level members. That means you can use only static variables inside any static method.
We can create static method when it does not depends on instance of the class, static methods are class level methods not instance level methods. So whenever your method is not going to change the status of the instance then you can declare that method as static.
You can create static methods for utility classes, helper classes. For example if you want to write error log to a file you can create static method as shown below.
class Helper
{
static string filePath = @"D:\Log\Error.txt";
public static void WriteErrorLog(string sError)
{
//Logic to write Error to Log
}
}
class Program
{
static void Main(string[] args)
{
Helper.WriteErrorLog("This is an Error");
}
}
As shown above Helper class has WriteErrorLog() method which just writes the error to Log file, so I declared it as static method. Again I am repeating you can access only static variables in the static method.
Static Class:
If any class contains only static variables, static properties, static methods….etc we can declare that class has static class. That means static class contains only static members, we cannot add any Non-Static members to static class.
Declare static class as shown below.
public static class Helper
{
static string filePath = @"D:\Log\";
public static string FileName { get; set; }
public static void WriteErrorLog(string sError)
{
//Logic to write Error to Log
}
}
class Program
{
static void Main(string[] args)
{
Helper.WriteErrorLog("This is an Error");
}
}
Static class members can be access through class name only not with the instance of the class.
Static Variables, Static Methods and Static Classes are useful for generic operations like mathematical operations.