Parametric Polymorphism in C# Flashcards
What are the upsides of C# generics?
no type erasure in binaries, which enables:
- Better optimization (for example, no need for boxing and unboxing)
- Better reflection support
What are the downsides of C# generics? and what are the solutions?
It might have caused code segment to dramatically increase in size
Solution #1 – Types are instantiated on demand (at run-time – CLR feature)
Solution #2 – All reference types share the same IL code
What is the default C# generics variance?
no-variance.
What is a delegate in C#?
delegate is pretty much a function pointer type.
hence the syntax is easy to remember:
public delegate object ConversionDelegate(string d);
(same as a function just add delegate at the begining).
Does C# support co-variance and contra-variance for generics?
yes. only for interfaces and delegates not classes.
applies only for reference types.
What is the out generic modifier in C#?
Specifies that the type parameter is only used to return values from methods, hence covariance is safe!
What is the in generic modifier in C#?
Specifies that the type parameter is only used to pass arguments into the methods, hence contra-variant is safe!