Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- // Generic interface
- interface IRepository<T>
- {
- void Add(T item);
- void Remove(T item);
- IEnumerable<T> GetAll();
- }
- // Class implementing the generic interface for a specific type
- class Repository<T> : IRepository<T>
- {
- private List<T> _items = new List<T>();
- public void Add(T item)
- {
- _items.Add(item);
- }
- public void Remove(T item)
- {
- _items.Remove(item);
- }
- public IEnumerable<T> GetAll()
- {
- return _items;
- }
- }
- class Program
- {
- static void Main()
- {
- // Using the generic interface with int
- IRepository<int> intRepository = new Repository<int>();
- intRepository.Add(1);
- intRepository.Add(2);
- foreach (var item in intRepository.GetAll())
- {
- Console.WriteLine($"Integer: {item}");
- }
- // Using the generic interface with string
- IRepository<string> stringRepository = new Repository<string>();
- stringRepository.Add("Hello");
- stringRepository.Add("World");
- foreach (var item in stringRepository.GetAll())
- {
- Console.WriteLine($"String: {item}");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement