Static classes and static class members in C# explained

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

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

serverpoint hosting banner

You might also like More from author

Leave A Reply

Your email address will not be published.