Search code examples
c#staticstatic-class

Is marking a class as static a binary breaking change?


I have a utility class that only contains static methods. If I mark the class as static, is that a breaking binary change?

I've compared the IL for both, and besides the loss of the default constructor (that should never be used), I can see the following differences...

Class not marked as static:

.class public auto ansi beforefieldinit MyNamespace.MyClass

Class marked as static:

.class public auto ansi abstract sealed beforefieldinit MyNamespace.MyClass

I can't see why that would be a breaking change, but...?


Solution

  • It depends on the usage of your class by other code: potential usage of static classes is a lot more restricted than that of non-static ones.

    • If the class has been used like static even though it hasn't been static at the time, the code is not going to break.
    • If the class has been used like non-static one, i.e. has been inherited or instantiated, then the change is breaking.

    Since in general you cannot guarantee that your non-static class is used in a certain way, making a formerly non-static class a static one should be considered a breaking change. If you have code outside your control that depends on your class, designate the old class obsolete, and make a new static class to be used instead of it.