C#实现Ruby的负数索引器
发布时间:2020-12-15 06:29:39 所属栏目:百科 来源:网络整理
导读:C#实现Ruby的负数索引器 public class InvertibleListT : ListT { 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
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#能够有所帮助 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |