当前位置: 移动技术网 > IT编程>开发语言>c# > C#实现Ruby的负数索引器

C#实现Ruby的负数索引器

2019年07月18日  | 移动技术网IT编程  | 我要评论
c#实现ruby的负数索引器 public class invertiblelist<t> : list<t> { pub

c#实现ruby的负数索引器

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);

代码很简单,使用也很方便,希望对大家学习c#能够有所帮助

如您对本文有疑问或者有任何想说的,请 点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网