What are Static Classes and Static Methods? (C# Programming)

A static class is basically the same as a non-static class, but there is one difference: a static class cannot be instantiated. In other words, you cannot use the new keyword to create a variable of the class type. Because there is no instance variable, you access the members of a static class by using the class name itself.

A static class can be used as a convenient container for sets of methods that just operate on input parameters and do not have to get or set any internal instance fields. For example, in the .NET Framework Class Library, the static System.Math class contains several methods that perform mathematical operations, without any requirement to store or retrieve data that is unique to a particular instance of the Math class. (MSDN)
In many places these static classes like the Math class above are called Utility Classes in the programming world. When you hear the term “Utility Class”, most probably it would be safe to assume that the mentioned class is a static class.
Most of the time you will be seeing class instances being created in a way like this:

MyClass MC = new MyClass();

or like this:

MyClass MC = new MyClass(myParameterValue);

And then, you will see method calls using this class instance (object) like below:

int someValue = MC.myMethod(); 
These classes are the classes that have instance constructors, these are the classes that can be instanciated. These are non-static classes.

But static classes are something different. They are defined with the reserved word “static” and they can not be instanciated. In addition, you don't need to call a static method in a static class using an instance of that class (which is as I just said not possible anyway).

You declare static class members by using the “static” keyword before the return type of the member:


public static void myStaticMethod() { }

Assuming we have a static class “MyStaticClass” and a static method in this class which is named “myStaticMethod” this is how you can invoke that static method:

int someValue = MyStaticClass.myStaticMethod(); (Note that I have not created a class instance)

Main features of a static class:
-They contain only static members (static methods, static properties etc.)
-They can not be instantiated
-They are sealed (other classes can not inherit from static classes)
-They cannot inherit from any class except “Object”.

-Static classes cannot contain an instance constructor; however, they can contain a static constructor.

Some Points to Note:
A non-static class can contain static methods, fields, properties, or events. The static member is callable on a class even when no instance of the class has been created. The static member is always accessed by the class name, not the instance name.
Static methods and properties cannot access non-static fields and events in their containing type, and they cannot access an instance variable of any object unless it is explicitly passed in a method parameter.

It is more typical to declare a non-static class with some static members, than to declare an entire class as static. Two common uses of static fields are to keep a count of the number of objects that have been instantiated, or to store a value that must be shared among all instances.

Static methods can be overloaded but not overridden, because they belong to the class, and not to any instance of the class.

Hope this helps someone.

Good luck,
Serdar.

Leave a Reply

Your email address will not be published. Required fields are marked *