0
0
C Sharp (C#)programming~10 mins

Generic constraints (where clause) in C Sharp (C#) - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to declare a generic class with a constraint that T must be a class.

C Sharp (C#)
public class Repository<[1]> where T : class { }
Drag options to blanks, or click blank then click option'
AT
BU
CV
DK
Attempts:
3 left
💡 Hint
Common Mistakes
Using a different letter in the constraint than in the generic declaration.
Forgetting to add the where clause after the generic declaration.
2fill in blank
medium

Complete the code to declare a generic method with a constraint that T must have a parameterless constructor.

C Sharp (C#)
public void CreateInstance<[1]>() where T : new() { T obj = new T(); }
Drag options to blanks, or click blank then click option'
AU
BT
CV
DK
Attempts:
3 left
💡 Hint
Common Mistakes
Using a different letter in the constraint than in the method declaration.
Omitting the new() constraint when creating a new instance.
3fill in blank
hard

Fix the error in the generic class declaration by completing the constraint so that T must inherit from BaseEntity.

C Sharp (C#)
public class Manager<[1]> where T : BaseEntity { }
Drag options to blanks, or click blank then click option'
AU
BK
CV
DT
Attempts:
3 left
💡 Hint
Common Mistakes
Using a different letter in the constraint than in the generic declaration.
Forgetting to specify the base class in the constraint.
4fill in blank
hard

Fill both blanks to declare a generic method where T must be a struct and U must implement IDisposable.

C Sharp (C#)
public void Process<[1], U>() where [1] : struct where U : [2] { }
Drag options to blanks, or click blank then click option'
AT
BU
CIDisposable
DBaseEntity
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up the type parameter names and constraints.
Using a class name instead of an interface for the IDisposable constraint.
5fill in blank
hard

Fill all three blanks to declare a generic class where T must be a class, U must inherit from BaseEntity, and V must have a parameterless constructor.

C Sharp (C#)
public class Container<[1], [2], V> where T : class where U : BaseEntity where V : [3] { }
Drag options to blanks, or click blank then click option'
AT
BU
Cnew()
DV
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong parameter names in the constraints.
Forgetting the new() constraint for V.