Posts Tagged ‘Type Safety’

Why I don’t see static extension support for constructors in C# coming

I was hoping C# would someday add static extension support for constructors too (apart from classic methods), that would help a lot in source-level compatibility layers (for example at my WPF_Compatibility project for WPF syntax in Silverlight, which one can find under the ClipFlair source base).

However, according to :​2007/01/​net-calling-base-constructors-i​n-c.html

Anders Hejlsberg (father of Delphi and C#) said in an e-mail:

The problem with Delphi’s model allowing constructors to be called on an already constructed object) is that it makes it impossible to have provably immutable objects. Immutability is an important concept because it allows applications to hand objects to an external party without first copying those objects and still have a guarantee that the objects won’t be modified. If constructors can be called on already constructed objects it obviously isn’t possible to make such guarantees. In Delphi’s case that may be ok since Delphi doesn’t really make type safety guarantees anyway (you can cast any object reference to a pointer-to-something and start poking away), but .NET goes further with type safety and this would be a big hole.

He was replying on why one can’t call an ancestor’s constructor directly in C#, but I guess this covers the case of injecting more constructors into ancestors via the static extension method mechanism of recent C# versions.

%d bloggers like this: