本帖最后由 张旭辉 于 2014-6-2 17:51 编辑
C#索引器的作用 C#通过提供索引器,可以象处理数组一样处理对象。特别是属性,每一个元素都以一个get或set方法暴露。- public class Skyscraper
- {
- Story[] stories;
- public Story this [int index]
- {
- get
- {
- return stories [index];
- }
- set
- {
- if (value != null)
- {
- stories [index] = value;
- }
- }
- }
- //...
- }
- Skyscraper empireState = new Skyscraper (/*...*/);
- empireState [102] = new Story ("The Top One", /*...*/);
复制代码
|