索引器的目的是什么? [英] What the purpose of Indexers?

查看:74
本文介绍了索引器的目的是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

不了解索引器的目的.示例:

Not understand the purpose of Indexers. Example:

class MyCls
{
   object[] myArr = new object[];
   public object this[int i]
   {
      get{return myArr[i];}
      set{myArr[i] = value;}
   }
}


为什么不只为myArr创建一个属性并通过它的属性访问它呢?


Why not just create a property for myArr and access it through it''s property?
What''s the advantage and/or purpose of using an Indexer?

推荐答案

对!为什么不只创建具有唯一名称的属性?没有关于此功能的原则.

Right! Why not just create a property with the distinct name? There is nothing principal about this feature.

这仅是语法糖,请参见 http://en.wikipedia.org/wiki/Syntactic_sugar [ ^ ].

This is nothing more than the syntactic sugar, see http://en.wikipedia.org/wiki/Syntactic_sugar[^].

作为语法糖,这是非常好的糖,它遵循其他语言的传统.

Being the syntactic sugar, this is very nice sugar, following nice tradition from other languages.



请参考以下链接

索引器教程
hi
please refer below links

indexer tutorial


这篇关于索引器的目的是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆