C# 索引器的意义或好处是什么?
阅读一些代码并偶然发现了这个我以前从未见过的片段:
public SomeClass {
public someInterface this[String strParameter] {
get {
return SomeInternalMethod(strParameter);
}
}
}
它看起来像这样调用:
SomeClass _someClass = new SomeClass();
SomeInterface returnedValue = _someClass["someString"];
我感兴趣的是这个函数在哪里合适或者以这种风格编写的意图是什么。例如,为什么这比简单地调用函数更可取?
Doing some code reading and stumbled upon this snippet that I haven't seen before:
public SomeClass {
public someInterface this[String strParameter] {
get {
return SomeInternalMethod(strParameter);
}
}
}
It looks like it is called as follows:
SomeClass _someClass = new SomeClass();
SomeInterface returnedValue = _someClass["someString"];
I am interested in where this function would be appropriate or what the intent of writing in this style. For example why would this be preferred over simply calling the function?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
请参阅语言规范第 10.9 节,其中指出:
索引器 是一个成员,它使对象能够像数组一样被索引。
索引器和属性在概念上非常相似,但在以下方面有所不同:
See the language specification, section 10.9, which states:
An Indexer is a member that enables an object to be indexed in the same way as an array.
Indexers and properties are very similar in concept, but differ in the following ways:
似乎很多答案都集中在索引器是什么,而不是为什么要使用索引器。
就我而言,这是使用索引器的动机:
您正在开发一个具有某种集合的类,但您希望该类向用户(消费者)显示类)就好像它是一个集合。
我能想到的最好的例子是 ADO.NET 中的 DataRow 类。如果您想获取
DataRow
的第五个单元格的值,可以使用DataRow.Item[4]
或DataRow[4]
。后一种形式是一种方便且合乎逻辑的快捷方式,它很好地展示了您想要使用索引器的原因。对于用户来说,DataRow
可以被认为只是单元格的集合(尽管它实际上不止于此),因此能够直接获取和设置单元格值是有意义的,而无需必须记住,您实际上正在获取/设置一个Item
。希望有帮助。
It seems like a lot of the answers are focusing on what an indexer is, not why you would want to use one.
As far as I'm concerned, here is the motivation to use an indexer:
You are working on a class that has a collection of some sort, but you want the class to appear to users (consumers of the class) as if it is a collection.
The best example I can think of is the
DataRow
class in ADO.NET. If you want to get the value of the fifth cell of aDataRow
, you can either useDataRow.Item[4]
orDataRow[4]
. The latter form is a convenient and logical shortcut, and it shows off pretty nicely why you'd want to use an indexer. To the user, theDataRow
can be thought of as just a collection of cells (even though it is really more than that), so it makes sense to be able to get and set cell values directly, without having to remember that you are actually getting/setting anItem
.Hope that helps.
在许多情况下,“索引”语法很有意义。如果 SomeClass 代表某种集合,则它特别有用。
In many cases, the 'index' syntax makes a lot of sense. It is particularly useful if the SomeClass represents some sort of collection.
正如您在问题中提到的那样,它允许您进行关联数组查找(又名“字典样式”)。
这就是重点。有些人喜欢这样,特别是来自内置它的语言的人,例如 Python 或 PHP
It allows you to do associative array lookups (a.k.a. "dictionary style"), just as you mentioned in your question.
And that's the whole point. Some people like that, particularly people coming from languages that have it built in, like Python or PHP
“this”关键字是
来自 http://msdn.microsoft.com 的 索引器/en-us/library/6x16t2tx.aspx
“索引器允许像数组一样对类或结构的实例进行索引。索引器类似于属性,只是它们的访问器采用参数。”
The "this" keyword is an indexer
from http://msdn.microsoft.com/en-us/library/6x16t2tx.aspx
"Indexers allow instances of a class or struct to be indexed just like arrays. Indexers resemble properties except that their accessors take parameters."
您之前可能已经偶然发现了类似的东西:
索引器是实现集合的对象的“主键”。这只是编写 .GetValue(index) 这样的函数的一种简写方式 - 语法糖,如果你愿意的话。但这也表明了意图。
You may have already stumbled across something similar before:
The indexer is the "primary key" of an object that implements a collection. It's just a shorthand way for writing a function like .GetValue(index) - syntactic sugar, if you want. But it also makes the intent clear.
它是索引运算符
[]
的实现。It an implementation of the index operator
[ ]
.这是操作员过载。如果您正在编写一个集合类,例如使用数组表示法访问它是有意义的,例如集合[someIndex],则很有用。
您当然可以编写一个等效的 collection.GetElement(someIndex) 函数,但这是一个样式/可读性的问题。
It's an operator overload. Useful if you are writing a collection class for example where it would make sense to access it using array notation, e.g. collection[someIndex].
You could of course write a collection.GetElement(someIndex) function equivalent, but it's a style/readability thing.
那么您可以在键值对类中使用此方法。
我不确定 c# 中的类似类是什么,但在 c++ STL 中,有一个映射类,您可以在其中使用
SomeObject["key"]
调用该方法,它将返回“值”与该键相关联。Well you could use this method in a key-value pair class.
I am not sure what the analogous class is in c#, but in c++ STL, there is the map class where you call the method with the
SomeObject["key"]
and it will return the "value" associated with that key.这就是所谓的索引器,它们允许您使用 List<>、ArrayList、Dictionary<> 等。以及使用数组语法的所有其他集合。
它只是语法糖,但如果使用得当,它会提供一定的可读性。
That's called an Indexer, they allow you to use List<>, ArrayList, Dictionary<> and all the other collections using an array syntax.
It's just syntactic sugar, but it gives some readability when used right.
在我看来,这只是语法上的便利。
你会在哪里不使用它:
你会在哪里受益:
如你所见,由于某种原因,你的数据是一个用字符串键索引的字典,你希望使用两个整数不定数来调用它并且仍然不想更改您的数据类型:
In my opinion, it's just a syntax convenience.
Where would you NOT use it:
Where would you benefit from it:
As you can see, for some reason, your data is a Dictionary indexed with a string key and you wish to call this with two integer indecies and still do not want to change your data type: