Generic Interfaces
Context: A generic interface defines members whose types depend on one or more type parameters. Classes that implement the interface specify the actual types. This allows creating flexible and reusable contracts.
Usage Example
Section titled “Usage Example”using System;
public interface IRepository<T>{ T Get(int id); void Add(T item);}
public class ProductRepository : IRepository<Product>{ public Product Get(int id) { return new Product { Id = id, Name = "Product " + id }; }
public void Add(Product item) { Console.WriteLine($"Adding {item.Name}"); }}
public class Product{ public int Id { get; set; } public string Name { get; set; }}
class Program{ static void Main() { IRepository<Product> repo = new ProductRepository(); Product p = repo.Get(1); Console.WriteLine(p.Name); repo.Add(p); }}Output console
Section titled “Output console”dotnet runProduct 1Adding Product 1Important notes
Section titled “Important notes”- A class can implement the same generic interface with different types.
- The interface can have multiple type parameters (e.g.,
IDictionary<TKey, TValue>).
Real-world usage example
Section titled “Real-world usage example”IEnumerable<T> – The fundamental interface for all .NET collections.
See .NET docs on generic interfaces.