手机
当前位置:查字典教程网 >编程开发 >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#数组初始化简析

C#实现任意数据类型转成json格式输出

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

C#+MO实现一个道路编辑软件(刚开始)

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

c#实现输出本月的月历

C#索引器简单实例代码

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

精品推荐
分类导航