A generic interface is an interface that has one or more type parameters. It allows you to define methods and properties that work with any data type specified when the interface is implemented.
Click to reveal answer
beginner
How do you declare a generic interface with one type parameter named T?
You declare it like this:
public interface IExample<T> { void DoSomething(T item); }
Click to reveal answer
intermediate
Why use generic interfaces instead of non-generic ones?
Generic interfaces provide type safety and code reuse. They let you write flexible code that works with any data type without losing the benefits of compile-time type checking.
Click to reveal answer
beginner
How do you implement a generic interface in a class?
You specify the type when implementing the interface. For example:<br><pre>public class MyClass : IExample<int> {<br> public void DoSomething(int item) {<br> // implementation<br> }<br>}</pre>
Click to reveal answer
intermediate
Can a generic interface have multiple type parameters? Give an example.
Yes, it can. Example:
public interface IPair<T1, T2> { T1 First { get; } T2 Second { get; } }
Click to reveal answer
What does the <T> mean in a generic interface declaration?
AIt is a placeholder for a data type specified later
BIt is a method name
CIt is a variable
DIt is a keyword to create an object
✗ Incorrect
The <T> is a placeholder for a type that will be specified when the interface is implemented.
How do you specify the type when implementing a generic interface?
ABy providing the type in angle brackets after the interface name
BBy declaring a new interface
CBy using the 'new' keyword
DBy casting the interface
✗ Incorrect
You specify the type by writing the interface name followed by the type in angle brackets, like IExample.
Which of these is a valid generic interface declaration?
Apublic interface ITest { void Do(T item); }
Bpublic class ITest<T> { void Do(T item); }
Cpublic interface ITest<T> { void Do(T item); }
Dpublic interface ITest<T> { T Do(); }
✗ Incorrect
Option C correctly declares a generic interface with a method using the generic type T.
Can a generic interface have properties using the generic type?
AOnly static properties can use the generic type
BYes, properties can use the generic type
CNo, generic interfaces cannot have properties
DNo, only methods can use the generic type
✗ Incorrect
Generic interfaces can have properties that use the generic type parameter.
What is the main advantage of using generic interfaces?
AThey allow private methods in interfaces
BThey make code run faster
CThey reduce the number of classes needed
DThey allow code reuse with type safety
✗ Incorrect
Generic interfaces allow writing reusable code that works with any type while keeping type safety.
Explain what a generic interface is and why it is useful.
Think about how you can write one interface that works with many data types.
You got /4 concepts.
Describe how to implement a generic interface in a class with an example.
Remember to specify the type in angle brackets after the interface name.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of a generic interface in C#?
easy
A. To allow the interface to work with any data type specified later
B. To restrict the interface to only one specific data type
C. To make the interface private and inaccessible outside its class
D. To automatically implement all methods without code
Solution
Step 1: Understand generic interfaces
Generic interfaces use placeholders (type parameters) to allow flexibility in the data types they work with.
Step 2: Identify the purpose
This flexibility means the same interface can be reused with different types, making code more reusable and type-safe.
Final Answer:
To allow the interface to work with any data type specified later -> Option A
Quick Check:
Generic interface = flexible type [OK]
Hint: Generic interfaces use placeholders for types [OK]
Common Mistakes:
Thinking generic interfaces fix one type only
Confusing generic interfaces with access modifiers
B. Class Repository must specify the type parameter when implementing IRepository
C. Method Add should return a value
D. Class Repository cannot implement interfaces
Solution
Step 1: Check interface implementation
The interface IRepository is generic and requires a type parameter <T>. The class Repository implements it without specifying this type.
Step 2: Identify the fix
The class must specify the type parameter, for example IRepository<object>, to correctly implement the interface.
Final Answer:
Class Repository must specify the type parameter when implementing IRepository -> Option B
Quick Check:
Generic interface requires type parameter [OK]
Hint: Generic interfaces need type parameters when implemented [OK]
Common Mistakes:
Omitting type parameter in implementation
Assuming interface methods must return values
Thinking classes can't implement interfaces
5. Given the generic interface and classes below, what will be the output?
interface ITransformer<T> { T Transform(T input); } class IntDoubler : ITransformer<int> { public int Transform(int input) => input * 2; } class StringRepeater : ITransformer<string> { public string Transform(string input) => input + input; } var doubler = new IntDoubler(); var repeater = new StringRepeater(); Console.WriteLine(doubler.Transform(5)); Console.WriteLine(repeater.Transform("Hi"));
hard
A. 5
HiHi
B. 55
HiHi
C. 10
Hi
D. 10
HiHi
Solution
Step 1: Analyze IntDoubler.Transform
This method doubles the integer input. For input 5, output is 5 * 2 = 10.
Step 2: Analyze StringRepeater.Transform
This method repeats the string input twice. For input "Hi", output is "Hi" + "Hi" = "HiHi".
Final Answer:
10
HiHi -> Option D
Quick Check:
Int doubled = 10, string repeated = HiHi [OK]
Hint: Trace each Transform method with given input [OK]
Common Mistakes:
Mixing outputs of different classes
Forgetting string concatenation doubles text
Assuming Transform returns original input
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.