📖 Scenario: You are building a simple system to store and display items of different types. You want to use a generic interface to handle items in a flexible way.
🎯 Goal: Create a generic interface called IItemHolder<T> with a method to get the item. Then implement this interface in a class and display the stored item.
📋 What You'll Learn
Create a generic interface IItemHolder<T> with a method T GetItem().
Create a class ItemHolder<T> that implements IItemHolder<T>.
Add a constructor to ItemHolder<T> to store an item of type T.
Implement the GetItem() method to return the stored item.
Create an instance of ItemHolder<string> with the value "Hello World".
Print the item returned by GetItem().
💡 Why This Matters
🌍 Real World
Generic interfaces let you write flexible code that works with many types without repeating yourself. For example, you can create containers or handlers for different data types easily.
💼 Career
Understanding generic interfaces is important for writing reusable and maintainable code in professional C# development, especially in libraries and frameworks.
Progress0 / 4 steps
1
Create the generic interface
Create a generic interface called IItemHolder<T> with a method T GetItem().
C Sharp (C#)
Hint
Use the interface keyword and add a generic type parameter <T>. Define a method T GetItem() inside.
2
Create the class that implements the interface
Create a class called ItemHolder<T> that implements the interface IItemHolder<T>. Add a private field item of type T and a constructor that takes a parameter item of type T and stores it.
C Sharp (C#)
Hint
Use class ItemHolder<T> : IItemHolder<T>. Add a private field item and a constructor to set it.
3
Create an instance and call the method
Create an instance of ItemHolder<string> called holder with the value "Hello World". Then call holder.GetItem() and store the result in a variable called result.
C Sharp (C#)
Hint
Create holder using new ItemHolder<string>("Hello World"). Call GetItem() and save to result.
4
Print the result
Write a line to print the variable result to the console.
C Sharp (C#)
Hint
Use Console.WriteLine(result); inside a Main method to print the value.
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.