As a programmer, you may have used Static Classes and Static Class Members in C# or other programming languages.
Static member variables and static functions of a normal class can be accessed without creating an instance of the class. But the respective classes are not required to be static.
See below a sample with static member variables and Static functions in C#
//non-static class containing static member variables
class NormalClass
{
//static member variable
private static intnumber;
//a static member function in C#
public static int Add(intx,inty)
{
number = x+y;
return number;
}
public int Substract(intx,inty)
{
number = x-y;
return number;
}
}
As you saw in the above sample, the function Add is static and can be accessed without an instance variable of the class.We can access static members(variables and functions) of the class by using ‘.’ operator.
The function Substract is an instance function and can be accessed only through an instance of the class as below,
NormalClass.Add(5, 10);
NormalClass class1 = new NormalClass();
class1 .Substarct(5,10);
What is the Benefit of the Static class
What is a Static Class & What Difference will a Static Class make?
Static classes are classes declared with the static keyword.
public static class StaticSampleClass
The main difference is that A static class can not be instantiated.
For example, think about a utility class which contains only utility functions.Utility functions are stateless in nature and hence no need of a class object to access it.
Hence Utility classes are always the best candidate for static classes.This avoids unnecessary instance creation, no need of disposing and also simple to use with reduced lines of code.
Static classes in C# Sample
See an example of a static class in C# below,
static class Utility
{
//static member variable
private static int number;
private int nonStatic; //wrong- a static class can not have non static mmembers
//C# static member function
internal static int Add(int x,int y)
{
number = x+y; // can access static variables or functions.
nonStatic = x+ y; // wrong- variable nonStatic is not accessible
return number;
}
//C# static member function
internal static int Substract(int x,int y)
{
return x-y;
}
// static member function in C#
internal static int Divide(int x,int y)
{
return x/y;
}
}
The internal static method of the static Class Utility can be accessed from outside of the class, simply as below
Utility.Add(20,10);
Utility.Substract(20,10);
Utility.Divide(20,10);
Static classes can have only static members. Static methods can access only static variables or functions.
Static classes in C# are by default internal to the project.No need to explicitly mention as internal. But if you want to access static class members outside of the project you need to use the public keyword explicitly as shown be
public static class Utility
{
publicstaticintAdd(intx,inty)
{
returnx+y;
}
publicstaticintSubstract(intx,inty)
{
returnx-y;
}
publicstaticintDivide(intx,inty)
{
returnx/y;
}
}
Summary
In this post, we covered Static classes in C# and how static member variables and static member functions are declared and used in C#.Hope you find this post useful. Comment your valuable feedback below.
Happy Coding!
You may like to read Static Class Vs Singleton
Leave a Reply