HashSet with Index Access

后端 未结 5 1386
轻奢々
轻奢々 2021-01-07 11:58

I would need a data structure that

  1. Allows me to add/item to it
  2. Do not allow duplication
  3. access the collection via index

I am t

5条回答
  •  走了就别回头了
    2021-01-07 12:35

    does it work?

    class MyHashSet : HashSet
    {
        public T this[int index]
        {
            get
            {
                int i = 0;
                foreach (T t in this)
                {
                    if (i == index)
                        return t;
                    i++;
                }
                throw new IndexOutOfRangeException();
            }
        }
    }
    

提交回复
热议问题