.net初學之索引器

來源:互聯網
上載者:User

標籤:

1.文法(可有可無)[存取修飾詞]  資料類型  this[資料類型  標識符]{    get{}    set{}}e.g:
 1 class Program 2     { 3         static void Main(string[] args) 4         { 5             Indexer ind = new Indexer(5); 6             ind[0] = new animal("貓科", " 豹子"); 7             ind[1] = new animal("貓科", " 豹子1"); 8             ind[2] = new animal("貓科", " 豹子2"); 9             ind[3] = new animal("貓科", " 豹子3");10             ind[4] = new animal("貓科", " 豹子4");11  12             for (int i = 0; i < 5; i++)13             {14                 Console.WriteLine(ind[i].show());15             }16         }17     }18  19     public class animal20     {21         private string kemu;22         public string Kemu23         {24             get { return kemu; }25             set { kemu = value; }26         }27         private string name;28         public string Name29         {30             get { return name; }31             set { name = value; }32         }33         public animal() { }34         public animal(string _kemu, string _name)35         {36             this.Kemu = _kemu;37             this.Name = _name;38         }39         public string show()40         {41             return "名字" + Name + "科目是" + Kemu;42         }43     }44     public class Indexer45     {46         public animal[] ani;47         public Indexer(int count)48         {49             if (count < 0)50                 Console.WriteLine("長度不合法");51             ani = new animal[count];52         }53  54         public animal this[int index]55         {56             get57             {58                 if (index > ani.Length)59                     return null;60                 return ani[index];61             }62             set { ani[index] = value; }63         }64     }

 

.net初學之索引器

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.