pondělí 10. ledna 2011

C# constructors

As usual, this is yet another in the series of complaints about C#, this time about constructors. Let me state right away that I do believe the syntax of constructors in C# is stupid. It comes as no surprise then that the syntax comes from C++. When it comes to syntax the authors of C and later C++ made quite a few stupid decisions, but I ain't gonna talk about the famous allaroundfix types of C or anything just now. So what do I hate about constructors in C#?
The fact that "A constructor looks very much like a method, but with no return type and a name which is the same as the name of the class". I don't mind the missing return type, what I do mind is the "name which is the same as the name of the class". Why? WHY? WHY?!? Why the fsck would I want to have to repeat the same name over and over again? Especially when constructors, unlike other methods are NOT inherited?


 class BlaBlaBlaBla : BleBleBle {
   public BlaBlaBlaBla( Something one, OrOther two) : base(one, two) {}

ring a bell? This is annoying by itself, but the name of the class repeated as the name of the constructor, makes it about 134.7845% worse. Imagine you need to create several subclasses of a common parent! Instead of copying the first ten or so lines of the first subclass and changing JUST AND ONLY the class name on top, you have to change the name on several places. And then if you happen to add another constructor to the base class and want the subclasses to have it as well ... no, it's not automatic, not even a copy&paste job ... you have to go and change the stupid constructor name in each and every silly subclass.

Thank you very much!

It's funny that if I want to "call" one constructor from another, I do not have to repeat the name

  public BlahBlahBlah(some parameters) : this() {

is enough. WHY? Or rather WHY isn't "this" enough on both places? Why couldn't it be

  public this(some parameters) : this() {

Well it could not. It would be too convenient.

Žádné komentáře: