今日看点:索引器可以重载_索引器
来源:互联网
2023-02-19 08:59:58
1、C#索引器的作用
2、C#通过提供索引器,可以象处理数组一样处理对象。特别是属性,每一个元素都以一个get或set方法暴露。
(资料图)
3、public class Skyscraper
4、{
5、Story[] stories;
6、public Story this [int index]
7、{
8、get
9、{
10、return stories [index];
11、}
12、set
13、{
14、if (value != null)
15、{
16、stories [index] = value;
17、}
18、}
19、}
20、//...
21、}
22、Skyscraper empireState = new Skyscraper (/*...*/);
23、empireState [102] = new Story ("The Top One", /*...*/);
24、【译注:索引器最大的好处是使代码看上去更自然,更符合实际的思考模式】
以上就是【索引器可以重载,索引器】相关内容。