Static class in C# is a special class that cannot be instantiated . In other words , you cannot create an object of a static class and cannot access static members using an object. You can access the public static members (methods, fields and properties) using dot operator (ClassName.MemberName).
In .Net framework, we have certain static classes such as Console, Math, Environment and ThreadPool, these classes have only static methods and the sole purpose of these classes is to group some related members and functionality together. Lets look at below Math class .
If you look at above Math class, then you can find all static methods in it, these methods can be accessed by using dot(.) operator (ClassName.MemberName) as follows.
1 2 3 |
int a, b; double z = Math.Round(1245.66); int x = Math.Min(a, b); |
Key Properties of Static Classes :
- The class only have static members (fields, methods, properties and events). You cannot have any instance field or instance method in it, the C# compiler throws an error if you do so.
- The class cannot be used to implement any interfaces because interface methods are callable only if there is any instance of the class available.
Create a Static Class in C# ?
You can create a static class by adding static modifier before the class name and after the access modifier as follows.
1 |
public static class MathsCalculator |
Static class and static method example:
In below example,
- We have created a static class as MathsCalculator .
- It has a static methods as static int CalculateMax(int[] arrNum) and static int CalculateMin(int[] arrNum) that takes an integer array and returns maximum and minimum value.
- In the main method , we are invoking both static method by using dot (.) operator.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
using System; namespace StaticClassDemo { //Static class with static keyword public static class MathsCalculator { public static int CalculateMax(int[] arrNum) { int max = 0; for (int i = 0; i < arrNum.Length; i++) { if (arrNum[i] > max) max = arrNum[i]; } return max; } public static int CalculateMin(int[] arrNum) { int min = arrNum[0]; for (int i = 0; i < arrNum.Length; i++) { if (arrNum[i] < min) min = arrNum[i]; } return min; } } class Program { static void Main(string[] args) { int[] nums = new int[] { 12, 33, 24, 55, 10, 66 }; //Call static class method directly using ClassName.Method int maxNum = MathsCalculator.CalculateMax(nums); int minNum = MathsCalculator.CalculateMin(nums); Console.Write("Numbers:"); for (int i = 0; i < nums.Length; i++) Console.Write("{0} ", nums[i].ToString()); Console.WriteLine(); Console.WriteLine("**************************"); Console.WriteLine("Calculate Min/Max Numbers:"); Console.WriteLine("**************************"); Console.WriteLine("Maximum Number is : {0}" , maxNum); Console.WriteLine("Minimum Number is : {0}", minNum); Console.Read(); } } } |
When we run above example, then below out put generated.
Here, in above we invoked static methods static int CalculateMax(int[] arrNum) and static int CalculateMin(int[] arrNum) method by using dot (.) operator as follows.
1 2 3 |
//Call static class method directly using ClassName.Method int maxNum = MathsCalculator.CalculateMax(nums); int minNum = MathsCalculator.CalculateMin(nums); |
How C# compiler handles it ?
C# compiler marks the class as Abstract and Sealed. If you look at the below IL (Intermediate Language) code of above sample, generated through ILDASM (IL Disassembler) tool , then you can notice that the C# compiler has converted the class MathsCalculator to a Abstract and Sealed class as .class public abstract auto ansi sealed beforefieldinit StaticClassDemo.MathsCalculator .
The purpose of having Abstract and Sealed modifier is as follows :
- Making the class as Abstract, the C# compiler ensures that no other class can create an instance of it .
- Making it as Sealed , the C# compiler ensures that no class can derive from it.
Important points about static class in C# :
Following are some of the key points.
- The class must have only static members (fields, methods, properties and events).
- C# compiler throws a compile error, if it has any other instance members in it.
- It cannot contain instance constructors, however it can have static constructors to initialize static fields.
- C# compiler automatics marks it as Abstract and Sealed during compilation time. Which ensures that, no one can create any instance of it nor it can be extended.
Thanks for your time 🙂 keep visit my other articles and also check out my article on Partial Class in C#
In VB it’s a Module and the members can’t have Shared written.