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#能够有所帮助
您可能感兴趣的文章:
如您对本文有疑问或者有任何想说的,请 点击进行留言回复,万千网友为您解惑!
网友评论