Home > Code Analysis > Code Issues specific for static classes

Code Issues specific for static classes

June 23rd, 2011

A static class can be used as a unit of organization for sets of utility functions that operate on input parameters and do not have to get or set any internal data. These functions can be accessed without creating an instance of the class. In this case, a static class can make your implementation simpler and faster because you do not have to create an object in order to call its methods.

A good example of a static class is the System.Math class, which provides constants and static methods for common mathematical functions.

You can use a static class as a convenient container for methods not associated with particular objects. A member of such classes can be used to separate data and behavior that is independent of any object identity: the data and functions do not change regardless of what happens to the object.

Having said that, there are some requirements necessary in order to declare static classes. CodeRush suggests several code issues for correct code writing when static classes declaration requirements are violated. These code issues allow you to see an error due to declaring a static class before compiling the code.

Here they are:

Cannot create an instance of static class

Cause:

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 when the program or namespace containing the class is loaded. This code issue has an error type, because your code won’t compile.

Sample:

CodeRush Code Issue - Cannot create an instance of static class

How to fix:

  • Use the static class name instead, and access its member directly without initiating it:

CodeRush Cannot Create An Instance Of Static Class Fix

Cannot inherit from static class

Cause:

Static classes are designed in a way that prevents inheriting from any class except System.Object. A static class implicitly inherits from type Object, you can’t cannot explicitly specify a base class or a list of implemented interfaces. Static classes are sealed by nature and therefore cannot be inherited.

Sample:

CodeRush Code Issue - Cannot inherit from static class

How to fix:

  • Do not derive a class from the static class:

CodeRush Cannot Inherit From Static Class Fix

Cannot declare instance member in a static class

Cause:

When a class declared as a static, it is indicating that it contains only static members, so it cannot declare instance members.

Sample:

CodeRush Code Issue - Cannot declare instance member in a static class

How to fix:

  • Mark the member as static:

CodeRush Cannot Declare Instance Member In A Static Class Fix

Static class cannot be abstract and Static class cannot be sealed

Cause:

A static class may not include a sealed or abstract modifier. Note however, that since a static class cannot be instantiated or derived from, it behaves as if it was both sealed and abstract.

Sample:

An abstract static class:

CodeRush Code Issue - Static class cannot be abstract

A sealed static class:

CodeRush Code Issue - Static class cannot be sealed

How to fix:

  • Do not mark a static class as ‘abstract’:

CodeRush Static Class Cannot Be Sealed Fix

  • Do not mark a static class as ‘sealed’:

CodeRush Static Class Cannot Be Sealed Fix

Static class cannot contain protected member

Cause:

A static class can only contain static members, which can be public or private. They cannot be marked ‘protected‘ or ‘protected internal‘. Since Static classes are sealed classes, and sealed classes can not be inherited, hence it can not contain protected members, because protected members can not be inherited.

Sample:

CodeRush Code Issue - Static class cannot contain protected member

How to fix:

  • Change the visibility of the static member, e.g.,:

CodeRush Static Class Cannot Contain Protected Member Fix

—–
Products: CodeRush Pro
Versions: 12.1 and up
VS IDEs: any
Updated: Nov/19/2012
ID: C106

Similar Posts:

  1. No comments yet. Be the first and leave a comment!