Multiple choice technology programming languages

Generic Constraints

  1. Can not use in structs

  2. A derivation constraint

  3. A default constructor constraint

  4. A reference/value type constraint

Reveal answer Fill a bubble to check yourself
B,C,D Correct answer
Explanation

Generic constraints in C# restrict type parameters. Derivation constraints specify a base class, constructor constraints require a parameterless constructor, and reference/value type constraints constrain to class or struct. Generic constraints CAN be used with structs - they're not limited to classes.

AI explanation

C# generic type parameters support several kinds of constraints: a derivation constraint (where T : SomeBaseClass) restricting T to a specific class or its subclasses, a default constructor constraint (where T : new()) requiring a public parameterless constructor, and a reference/value type constraint (where T : class or where T : struct) restricting T to reference or value types respectively. Generic constraints can be used with structs/generic types generally, so 'cannot use in structs' is false.