Default Value Expression
As you’ve seen repeatedly, the introduction of generics in .NET 2.0 took away a bunch of limitations that came from the mandatory use of System.Object in generic code, resulting in weaker typing than desired. With generics, it’s possible to use type parameters and write code in a fashion as generic as possible.
However, sometimes one needs to synthesize a special “default value” for a type parameter without knowing the type of the parameter until runtime. All that matters to set the scene is that we are defining a generic type with a type parameter T. In the definition of that type, we don’t know anything about T. It could be an int, a string, a Customer, whatever. The generic type can use T anywhere as if it were a ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access