Helper and utility classes are often designed to never be instantiated. In C# 1.0 developers had to enforce this manually by making the constructor private and sealing the class, but nothing prevented the class from accidentally containing instance members.
C# 2.0 adds the static modifier to classes. A static class cannot be instantiated or inherited, and the compiler enforces that it contains only static members.
Code
C#
static class MathHelper
{
public static double DegreesToRadians(double degrees)
{
return degrees * Math.PI / 180.0;
}
}C#
sealed class MathHelper
{
private MathHelper() { }
public static double DegreesToRadians(double degrees)
{
return degrees * Math.PI / 180.0;
}
}Notes
- The compiler will emit an error if you try to add instance members, inherit from, or instantiate a static class
- Static classes are also required for declaring Extension methods C# 3.0