Advertisement
Guest User

Untitled

a guest
Sep 25th, 2020
37
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.14 KB | None | 0 0
  1. namespace TestConsole
  2. {
  3.     using System;
  4.     using System.Collections;
  5.     using System.Collections.Generic;
  6.     using System.Linq;
  7.  
  8.     public class Collection
  9.     {
  10.         private Dictionary<string, IList> _dict;
  11.  
  12.         public Collection()
  13.         {
  14.             _dict = new Dictionary<string, IList>();
  15.         }
  16.  
  17.         public void Add<T>(string key, T item)
  18.             where T : IKek
  19.         {
  20.             var list = GetList<T>(key);
  21.             list.Add(item);
  22.         }
  23.  
  24.         public List<T> GetList<T>(string key)
  25.             where T : IKek
  26.         {
  27.             if (_dict.TryGetValue(key, out var list) && (list is List<T> concrecList))
  28.             {
  29.                 return concrecList;
  30.             }
  31.  
  32.             var newList = new List<T>();
  33.             _dict.Add(key, newList);
  34.             return newList;
  35.         }
  36.  
  37.         public IEnumerator<T> GetEnumerator<T>(string key)
  38.             where T : IKek
  39.         {
  40.             return GetList<T>(key).GetEnumerator();
  41.         }
  42.     }
  43.  
  44.     public interface IKek
  45.     {
  46.         public string Text { get; set; }
  47.  
  48.         public string Code { get; set; }
  49.     }
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement