Base types, Collections, Diagnostics, IO, RegEx…
So an interesting situation arose today I though I would share with everyone. I'd love your input on this issue as well, if you feel strongly one way or the other.
Curiously, the C# compiler (at least) allows you to write the following code:
public abstract class Test { int _val; public Test() {_val = 9;} public Test(int i) {_val = i;} public virtual int Value { get { return _val; } } public override string ToString() { return _val.ToString("N"); }}
What's so interesting? Well, we have an abstract class with a public .ctor (indeed, two of them). The fact that it's public is a bit of an odd thing, given that no-one can actually instantiate an instance of this class: it's abstract! Now of course, subclasses can reference the public .ctor like so:
class Test2 : Test { public Test2() : base(12) {} public Test2(int i) : base(i) {}}
But the fact the the .ctor is public on Test is misleading. It gives the impression that it can be called from any code, whereas it's only use is the above: from a subclass. Because of this, it would have been far more suitable to write the following code in the base class:
public abstract class Test { int _val; protected Test() {_val = 9;} protected Test(int i) {_val = i;} public virtual int Value { get { return _val; } } public override string ToString() { return _val.ToString("N"); }}
The interesting part comes when we discovered today that some teams have changed APIs which originally had the public .ctor, to instead be protected. Traditionally, reducing the scope of a member in this way is considered a breaking change, but in this particular instance, there should be no consumer who is actually adversely affected: the only APIs that could originally take advantage of the public .ctor were subclasses, and they can still use the protected .ctor.
So the arguments for/against become:
Where do you sit?