How to Create Generic Interface in C# - Simple Guide
In C#, you create a generic interface by adding a type parameter in angle brackets after the interface name, like
interface IExample<T>. This allows the interface to work with any data type specified when implementing it. You define methods or properties using the generic type T inside the interface.Syntax
The syntax for creating a generic interface in C# includes the interface keyword followed by the interface name and a type parameter in angle brackets. Inside the interface, you use the generic type parameter to define methods or properties that work with any type.
- interface: keyword to declare an interface
- IExample<T>: interface name with generic type parameter
T - T: placeholder for any data type
csharp
interface IExample<T>
{
void DoSomething(T item);
T GetItem();
}Example
This example shows a generic interface IRepository<T> with methods to add and get an item. The Repository<T> class implements this interface for any type T. The example demonstrates how to use the generic interface with different data types like string and int.
csharp
using System;
interface IRepository<T>
{
void Add(T item);
T Get();
}
class Repository<T> : IRepository<T>
{
private T _item;
public void Add(T item)
{
_item = item;
}
public T Get()
{
return _item;
}
}
class Program
{
static void Main()
{
IRepository<string> stringRepo = new Repository<string>();
stringRepo.Add("Hello World");
Console.WriteLine(stringRepo.Get());
IRepository<int> intRepo = new Repository<int>();
intRepo.Add(42);
Console.WriteLine(intRepo.Get());
}
}Output
Hello World
42
Common Pitfalls
Common mistakes when creating generic interfaces include:
- Forgetting to specify the generic type parameter when implementing the interface.
- Using a different type parameter name in implementation, which causes confusion.
- Trying to use a generic type without specifying it, leading to compilation errors.
Always keep the generic type consistent and specify it explicitly when implementing.
csharp
/* Wrong: Missing generic type parameter */ // class WrongRepo : IRepository // { // public void Add(T item) { } // public T Get() { return default; } // } /* Right: Specify generic type parameter */ class RightRepo : IRepository<int> { private int _item; public void Add(int item) { _item = item; } public int Get() { return _item; } }
Quick Reference
Remember these key points when working with generic interfaces in C#:
- Use angle brackets
<>to declare generic type parameters. - Use the generic type inside interface methods or properties.
- Specify the type when implementing the interface.
- Generic interfaces increase code reusability and type safety.
Key Takeaways
Declare generic interfaces using angle brackets with a type parameter, e.g.,
IExample.Use the generic type parameter inside interface methods or properties to work with any data type.
Always specify the generic type when implementing the interface to avoid errors.
Generic interfaces help write reusable and type-safe code.
Avoid forgetting the type parameter or mixing type names between interface and implementation.