C# Static Class

Basic about Static Class

Static classes and class members are used to create data and functions that can be accessed without creating an instance of the class
Characteristic of static class .Net 2.0
  • Static class allows only static members
  • Static classes can not be instantiated
  • Static classes are sealed so they can not be inherited
  • Static classes can not allow derive the class or interfaces
  • Static class doesn’t allow instance ( non static ) members

All variables, methods inside static class are static

Static Classes

A class can be declared static, indicating that it contains only static members. It is not possible to create instances of a static class using the new keyword. Static classes are loaded automatically by the .NET Framework common language runtime (CLR) when the program or namespace containing the class is loaded.

Use a static class to contain methods that are not associated with a particular object. For example, it is a common requirement to create a set of methods that do not act on instance data and are not associated to a specific object in your code. You could use a static class to hold those methods.

The main features of a static class are:

Creating a static class is therefore much the same as creating a class that contains only static members and a private constructor. A private constructor prevents the class from being instantiated.

The advantage of using a static class is that the compiler can check to make sure that no instance members are accidentally added. The compiler will guarantee that instances of this class cannot be created.

Static classes are sealed and therefore cannot be inherited. Static classes cannot contain a constructor, although it is still possible to declare a static constructor to assign initial values or set up some static state.






Static classes can’t be instantiated in the first place, so even if you could declare non-static (instance) members, they can never be accessed. Since there really isn’t a point allowing it for that reason, the language simply prohibits it.

Bear in mind that static classes are just the classes, while there are two things that are directly related to non-static classes: the classes themselves, and the instances/objects of the classes.

A non-static class can have both static and non-static members so that the static members apply to the class, whereas the non-static members apply to the instances of that class.



Static class can’t contain non-static members because by definition it can’t be instantiated so there’s no possibility to use these members.

However, static members in non-static class can be used without having class instance – a bit different scenario, i.e. for utility methods or factory methods.



Static class is just a handy constraint to ensure the class doesn’t accidentally contain non-static members. It belies programmer’s intention on how that class should be used. It says: “This class should not be instantiated”.

You can have classes with static members and non-static members combined, which becomes a non-static class. All you need to do is to remove the static keyword from class definition. And that would give a different message about programmer’s intention on how the class should be used.

The only place where a static class and a non-static class makes a difference is that extension methods must be in a static class.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: