Impacts of new() contraint in C#

“Disecting the new() constraint” is very instructive: new() constraint has a big performance impact and we must know its implementation to understand why ctor exceptions are wrapped to a TargetInvocationException.

Dissecting the new() constraint in C#: a perfect example of a leaky abstraction – Dissecting the code

By the way, in same blog post, I’ve discovered that some well known static functions can be custom replaceable. It means you can locally redefined them. System.Activator.CreateInstance<T> is replaced by a faster one.

Beware, this is a completly not documented behavior than can be theoretically remove any time. Just to mitigate previous remark, this undocumented behavior is so important survived the Roslyn transition.

The implementation of anonymous methods in C# and its consequences

When we use anomymous lambda in C#, we can have to choose between passing values as parameter or using lambda capture. Second one is elegant and easy: we “just” us enclosing variables.

It is good to remember that capturing has a cost which should be understood and taken into account. For this, we can refer to : The implementation of anonymous methods in C# and its consequences – The Old New Thing.

Knowing this, we can think that C++ 11 solution is better. We have to choose wich variables are captured and if capture is a copy or a modifiable reference in capture specification block ([]).  And, by default, there is no capture. That way, overhead it is not a hazard but a deliberate choice. Perhaps for C# 7 but with breaking changes.