欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C#實現(xiàn)Ruby的負(fù)數(shù)索引器

 更新時間:2016年07月31日 09:35:53   投稿:hebedich  
這篇文章主要介紹了C#實現(xiàn)Ruby的負(fù)數(shù)索引器的相關(guān)代碼和使用方法,非常簡單實用,需要的朋友可以參考下

C#實現(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);

代碼很簡單,使用也很方便,希望對大家學(xué)習(xí)C#能夠有所幫助

相關(guān)文章

最新評論