Generic List Class in C #

I am currently creating my own base generic class of classes (to better understand how predefined work). The only problem I am facing is that I cannot reach the elements inside the array, as you usually do using " System.Collections.Generic.List".

GenericList<type> list = new GenericList<type>();
list.Add(whatever);

This works fine, but when I try to access "independently" I want to write :

list[0];

But this obviously does not work, since I am explicitly missing something in the code, what do I need to add to my fully-fledged generic class?

+5
source share
2 answers

He called indexer , written like this:

public T this[int i]
{
    get
    {
        return array[i];
    }
    set
    {
        array[i] = value;
    }
}
+12
source

I think all you have to do is implement IList<T>in order to get all the basic functionality

  public interface IList<T>  
  {

    int IndexOf(T item);

    void Insert(int index, T item);

    void RemoveAt(int index);

    T this[int index] { get; set; }
  }
+1
source

All Articles