Parametric Polymorphism in C# Flashcards

1
Q

What are the upsides of C# generics?

A

no type erasure in binaries, which enables:

  1. Better optimization (for example, no need for boxing and unboxing)
  2. Better reflection support
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What are the downsides of C# generics? and what are the solutions?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is the default C# generics variance?

A

no-variance.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is a delegate in C#?

A

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).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Does C# support co-variance and contra-variance for generics?

A

yes. only for interfaces and delegates not classes.

applies only for reference types.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is the out generic modifier in C#?

A

Specifies that the type parameter is only used to return values from methods, hence covariance is safe!

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is the in generic modifier in C#?

A

Specifies that the type parameter is only used to pass arguments into the methods, hence contra-variant is safe!

How well did you know this?
1
Not at all
2
3
4
5
Perfectly