作者ssccg (23)
看板C_Sharp
标题Re: [问题] indexer的疑问
时间Mon Feb 1 18:08:48 2010
※ 引述《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
1F:推 QQ29:对不起@@您一开始说的explicit imp 可否举个例子@@ 02/01 20:17
2F:→ QQ29:我怎麽写都会compile出错 说什麽public 不该这样写之类的 02/01 20:18
4F:推 loveme00835:你的interface决定了他的权限 02/02 02:54