2

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

Giles
  • 1,331
  • 1
  • 15
  • 30
  • 3
    If someone had inherited from MyNamespace.MyClass or created instances of it directly then it would be breaking – Trevor Pilley Dec 10 '14 at 11:28
  • @TrevorPilley: I wasn't expecting functional inheritance to be used by the clients - I was just thinking of whether normal static method calls via the class name would be broken - but it's a very good point nonetheless. Thanks. – Giles Dec 10 '14 at 11:34

3 Answers3

3

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.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • This is the answer that answered what I *meant* to ask best (if the class is used like a static) but all the other points are completely valid - thanks. – Giles Dec 10 '14 at 11:44
2

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.

Sriram Sakthivel
  • 72,067
  • 7
  • 111
  • 189
2

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.

Selman Genç
  • 100,147
  • 13
  • 119
  • 184
  • I'll take all the answers to tacitly indicate that normal calls on the static methods will be fine (I should have been clearer in my question), but this is the clearest answer - thanks. – Giles Dec 10 '14 at 11:40