Static classes and static class members in C# explained

The static keyword in the C# programming language allows you to define static and static .

A static class is similar to a class that is both abstract and sealed. The difference between a static class and a non-static class is that a static class cannot be instantiated or inherited and that all of the members of the class are static in nature. To declare a class as static, you should mark it with the static keyword in the class declaration.

There is no behavior in a static class or member, so there isn’t any point in allowing a static class to be inherited either. A static class can only have static members — you cannot declare instance members (methods, variables, properties, etc.) in a static class. You can have a static constructor in a static class but you cannot have an instance constructor inside a static class.

When to use a static class in C#

When should you go for a static class? Typically you can implement helper or utility classes as static classes since they don’t need to be instantiated or inherited and generally contain a collection of some reusable methods and properties. The following code listing illustrates what a static class in C# looks like.

You might also like
Leave A Reply

Your email address will not be published.