Re: [問題] indexer的疑問
※ 引述《QQ29 (我愛阿蓉)》之銘言:
: 再看indexer這個用法
: 書本上和msdn都舉了簡單的範例
: 他們的class中只有一個array 所以他們寫的get和set都是for這個array
: 我的疑問是 如果我有兩個array呢?
: 我該怎麼分別說我的indexer是for哪一個array...
: MSDN最下面好像想講這個但是很奇怪...
: http://msdn.microsoft.com/en-us/library/tkyhsw31.aspx
: 怎麼會是string型態
: 而且我在我this前面寫上ISomeInterface. compile就不會過...
: 我不曉得為什麼
加上ISomeInterface.,是interface的explicit implementation
跟indexer沒關係
那段是在說如果你的class同時實作兩個都有定義indexer的interface
而且indexer的signature一樣,那就必須用explicit implementation
: 還是說indexer只適用於 只有一個array在data member的class....
: 才能像她寫的:Indexers allow instances of a class or struct to be indexed just
: like arrays.
: 不過我覺得一定不是這樣 想不太通
: 請教一下各位
: 謝謝
indexer其實跟property很像,用存取變數的方式使用一個method
只是property只有一個名字,用object.PropertyName存取
而indexer可以多index參數,用object[index1]存取
index的type和數量、回傳的type都是可以自己定的
實際去存取的也不一定要是一個array
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 218.166.97.5
推
02/01 20:17, , 1F
02/01 20:17, 1F
→
02/01 20:18, , 2F
02/01 20:18, 2F
推
02/02 02:34, , 3F
02/02 02:34, 3F
推
02/02 02:54, , 4F
02/02 02:54, 4F
討論串 (同標題文章)
本文引述了以下文章的的內容:
完整討論串 (本文為第 2 之 2 篇):