Is marking a class as static a binary breaking change?

171 views Asked by At

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...?

3

There are 3 answers

1
Sergey Kalinichenko On BEST ANSWER

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.

0
Sriram Sakthivel On

Yes that is a breaking change. If the consumer of your library has subclassed your class, it will be broken.

Given that the class only contains bunch of static methods there is no point in inheriting it, but if someone did, that will stop their code from compiling.

Also note that you can't even declare a field of a type which is a static class. If the consumer had a field, property or something of your type, that will also break.

So the answer depends upon how the consumer library used it.

1
Selman Genç On

As you can see there are two modifiers added for static:

abstract: means you can't instantiate it

sealed: means you can't inherit from it

So in your code if you are instantiating this class or you have a type that inherits from it, your code will be broken.