手机
当前位置:查字典教程网 >编程开发 >C#教程 >C#实现Ruby的负数索引器
C#实现Ruby的负数索引器
摘要:C#实现Ruby的负数索引器publicclassInvertibleList:List{publicnewTthis[intindex]{...

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#能够有所帮助

【C#实现Ruby的负数索引器】相关文章:

C#: 引用变量与数值变量的区别

C# 获取属性名的方法

C#实现对AES加密和解密的方法

C# 面向对象的基本原则

c#实现无标题栏窗口的拖动

.NET实现:将EXE设置开机自动启动

c#实现输出本月的月历

C#多维数组学习使用

C#实现的几种委托方式介绍

C#拼接SQL语句 用ROW_NUMBER实现的高效分页排序

精品推荐
分类导航