手机
当前位置:查字典教程网 >编程开发 >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#实现任意数据类型转成json格式输出

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

使用C#实现阿拉伯数字到大写中文的转换

C#中通过API实现的打印类 实例代码

C#数组初始化简析

C#可选参数的相关使用

C#图片压缩的实现方法

C#实现的最短路径分析

C#实现协同过滤算法的实例代码

C# 面向对象的基本原则

精品推荐
分类导航