C#实现Ruby的负数索引器
C#实现Ruby的负数索引器
publicclassInvertibleList<T>:List<T>
{
publicnewTthis[intindex]
{
get
{
if(index>=0)returnbase[index];
if(Count+index<0)
thrownewIndexOutOfRangeException();
returnthis[Count+index];
}
set
{
if(index>=0)
base[index]=value;
else
{
if(Count+index<0)
thrownewIndexOutOfRangeException();
this[Count+index]=value;
}
}
}
}
使用方法:
InvertibleList<string>list=newInvertibleList<string>
{
"1",
"2",
"3",
"4",
"5",
};
list[-2]="asd";
list.ForEach(Console.WriteLine);
代码很简单,使用也很方便,希望对大家学习C#能够有所帮助
热门推荐
6 保研的祝福语简短
10 年轻20岁祝福语简短
11 朋友结婚祝福语信息简短
12 女孩婚礼贺卡祝福语简短
13 30段点歌简短祝福语
14 虎年春节祝福语图文简短
15 写给后妈祝福语大全简短
16 简短回复生日祝福语
17 校长送毕业祝福语简短
18 毕业立体贺卡祝福语简短