Recall & Review
beginner
What is a generic interface in C#?
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: <br>
public interface IExample<T> {<br> void DoSomething(T item);<br>}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:<br>
public interface IPair<T1, T2> {<br> T1 First { get; }<br> T2 Second { get; }<br>}Click to reveal answer
What does the <T> mean in a generic interface declaration?
✗ 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?
✗ 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?
✗ 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?
✗ Incorrect
Generic interfaces can have properties that use the generic type parameter.
What is the main advantage of using generic interfaces?
✗ 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.