C#實(shí)現(xiàn)Ruby的負(fù)數(shù)索引器
public class InvertibleList<T> : List<T>  {    public new T this[int index]    {      get      {        if (index >= 0) return base[index];        if (Count + index < 0)          throw new IndexOutOfRangeException();        return this[Count + index];      }      set      {        if (index >= 0)          base[index] = value;        else        {          if (Count + index < 0)             throw new IndexOutOfRangeException();          this[Count + index] = value;        }      }    }      }使用方法:
InvertibleList<string> list=new InvertibleList<string>      {        "1",        "2",        "3",        "4",        "5",      };      list[-2] = "asd";      list.ForEach(Console.WriteLine);代碼很簡(jiǎn)單,使用也很方便,希望對(duì)大家學(xué)習(xí)C#能夠有所幫助
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注