雷火电竞-中国电竞赛事及体育赛事平台

歡迎來到入門教程網(wǎng)!

C#教程

當前位置:主頁 > 軟件編程 > C#教程 >

C#實現(xiàn)Ruby的負數(shù)索引器

來源:本站原創(chuàng)|時間:2020-01-10|欄目:C#教程|點擊:

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#小數(shù)點格式化用法小結

欄    目:C#教程

下一篇:聊一聊C#接口問題 新手速來圍觀

本文標題:C#實現(xiàn)Ruby的負數(shù)索引器

本文地址:http://www.jygsgssxh.com/a1/C_jiaocheng/6312.html

網(wǎng)頁制作CMS教程網(wǎng)絡編程軟件編程腳本語言數(shù)據(jù)庫服務器

如果侵犯了您的權利,請與我們聯(lián)系,我們將在24小時內進行處理、任何非本站因素導致的法律后果,本站均不負任何責任。

聯(lián)系QQ:835971066 | 郵箱:835971066#qq.com(#換成@)

Copyright © 2002-2020 腳本教程網(wǎng) 版權所有