Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Animals : IEnumerable<Animal>
- {
- public void Add<T>(T animal) where T : Animal
- {
- var type = typeof(T);
- if (!_animals.ContainsKey(type)) _animals[type] = new List<Animal>();
- _animals[type].Add(animal);
- }
- public IEnumerable<T> GetAll<T>() where T : Animal
- {
- var type = typeof(T);
- return _animals.ContainsKey(type) ? (IEnumerable<T>) _animals[type] : Enumerable.Empty<T>();
- }
- public IEnumerable<Animal> GetAll()
- {
- var animals = new List<Animal>();
- foreach (var kvp in _animals)
- animals.AddRange(kvp.Value);
- return animals;
- }
- Dictionary<Type, List<Animal>> _animals = new Dictionary<Type, List<Animal>>();
- public IEnumerator<Animal> GetEnumerator() => GetAll().GetEnumerator();
- IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement