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

Generic constraints (where clause) in C Sharp (C#) - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Generic Constraints Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
Output of generic method with class constraint
Consider this C# generic method with a where clause restricting T to classes:

public void PrintTypeName<T>() where T : class { Console.WriteLine(typeof(T).Name); }

What will be printed when calling PrintTypeName<string>()?
C Sharp (C#)
PrintTypeName<string>();
AString
Bstring
CSystem.String
Dclass
Attempts:
2 left
💡 Hint
Remember that typeof(T).Name returns the exact type name with correct casing.
📝 Syntax
intermediate
2:00remaining
Identify the invalid generic constraint syntax
Which of the following generic method declarations has a syntax error in its where clause?
Avoid Method&lt;T&gt;() where T : struct {}
Bvoid Method&lt;T&gt;() where T : new() {}
Cvoid Method&lt;T&gt;() where T : int {}
Dvoid Method&lt;T&gt;() where T : class, new() {}
Attempts:
2 left
💡 Hint
Check if the constraint type is a valid generic constraint type.
🧠 Conceptual
advanced
2:00remaining
Understanding multiple generic constraints
Given the generic class declaration:

class Repository<T> where T : class, IDisposable, new() {}

Which of the following statements is true about T?
AT must be a class but does not need to implement IDisposable.
BT can be any type that implements IDisposable, including structs.
CT must be a value type with a parameterless constructor.
DT must be a reference type that implements IDisposable and has a parameterless constructor.
Attempts:
2 left
💡 Hint
Look carefully at all constraints combined with commas.
🔧 Debug
advanced
2:00remaining
Why doesn't this generic method modify the caller's variable?
Examine this method:

void Save<T>(T item) where T : new() { var instance = new T(); item = instance; }

Why doesn't this code modify the original item passed by the caller?
C Sharp (C#)
void Save<T>(T item) where T : new() { var instance = new T(); item = instance; }
ABecause method parameters are passed by value, so the assignment inside the method does not affect the caller's variable.
BBecause T must be a class to use 'new T()'.
CBecause 'item' is a method parameter and cannot be assigned to inside the method.
DBecause 'new()' constraint requires T to have a parameterless constructor but T might be abstract.
Attempts:
2 left
💡 Hint
Think about how method parameters behave in C#.
optimization
expert
2:00remaining
Optimizing generic constraints for performance
You have a generic method:

public void Process<T>(T item) where T : class { /* ... */ }

Which change to the where clause can improve performance by allowing the compiler to optimize better?
AAdd <code>new()</code> constraint to allow object creation.
BChange to <code>where T : struct</code> to avoid null checks.
CRemove the constraint entirely to allow any type.
DAdd <code>class, new()</code> constraints to allow object creation and reference type optimizations.
Attempts:
2 left
💡 Hint
Value types avoid null checks and boxing, improving performance.