C#實現(xiàn)Ruby的負數(shù)索引器
更新時間:2016年07月31日 09:35:53 投稿:hebedich
這篇文章主要介紹了C#實現(xiàn)Ruby的負數(shù)索引器的相關代碼和使用方法,非常簡單實用,需要的朋友可以參考下
C#實現(xiàn)Ruby的負數(shù)索引器
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#?使用Fluent?API?創(chuàng)建自己的DSL(推薦)
DSL領域專用語言是描述特定領域問題的語言,聽起來很唬人,其實不是什么高深的東西,下面通過實例代碼介紹下C#?使用Fluent?API?創(chuàng)建自己的DSL,感興趣的朋友參考下吧2021-12-12
c#打印預覽控件中實現(xiàn)用鼠標移動頁面功能代碼分享
項目中需要實現(xiàn)以下功能:打印預覽控件中,可以用鼠標拖動頁面,以查看超出顯示范圍之外的部分內容,下面就是實現(xiàn)代碼2013-12-12

