C# 多个索引器
是否可能有类似以下内容:
class C
{
public Foo Foos[int i]
{
...
}
public Bar Bars[int i]
{
...
}
}
如果没有,那么我可以通过哪些方法来实现这一目标? 我知道我可以创建名为 getFoo(int i) 和 getBar(int i) 的函数,但我希望用属性来做到这一点。
Is it possible to have something like the following:
class C
{
public Foo Foos[int i]
{
...
}
public Bar Bars[int i]
{
...
}
}
If not, then are what are some of the ways I can achieve this? I know I could make functions called getFoo(int i) and getBar(int i) but I was hoping to do this with properties.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
不是在 C# 中,不是。
但是,您始终可以从属性返回集合,如下所示:
IList; 有一个索引器,因此您可以编写以下内容:
在“return ...;”行上 您可以返回任何实现 IList的内容,但您可能会返回集合的只读包装器,请参阅 AsReadOnly() 方法。
Not in C#, no.
However, you can always return collections from properties, as follows:
IList<T> has an indexer, so you can write the following:
On the lines "return ...;" you can return whatever implements IList<T>, but you might what to return a read-only wrapper around your collection, see AsReadOnly() method.
这来自 C# 3.0 规范
“索引器重载允许类、结构或接口声明多个索引器,前提是它们的签名在该类、结构或接口中是唯一的。”
This from C# 3.0 spec
"Overloading of indexers permits a class, struct, or interface to declare multiple indexers, provided their signatures are unique within that class, struct, or interface."
有一种方法..如果你定义了 2 个新类型来允许编译器区分两个不同的签名...
那么要调用它你就必须写:
如果你写了一个隐式转换运算符:
那么你甚至不会必须这样做才能使用它,你可以只写:
即使两个索引器返回不同的类型,同样的事情也适用......
There IS a way.. if you define 2 new types to alow the compiler to distinguish the two different signatures...
Then to call it you would have to write:
And if you wrote an implicit conversion operator:
then you wouldn't even have to do that to use it, you could just write:
Same thing applies even if the two indexers return different types...
尝试我的 IndexProperty 类在同一类中启用多个索引器
< a href="http://www.codeproject.com/Tips/319825/Multiple-Indexers-in-Csharp" rel="nofollow">http://www.codeproject.com/Tips/319825/Multiple-Indexers- in-Csharp
Try my IndexProperty class to enable multiple indexers in the same class
http://www.codeproject.com/Tips/319825/Multiple-Indexers-in-Csharp
如果您尝试执行以下操作:
那么您需要在 Foo 和 Bar 类本身上定义索引器 - 即,将所有 Foo 对象放入 Foos 中,并使 Foos 成为直接支持索引的类型实例。
为了演示使用数组作为成员属性(因为它们已经支持索引器):
将允许您说:
If you're trying to do something like this:
then you need to define the indexers on the Foo and Bar classes themselves - i.e. put all the Foo objects inside Foos, and make Foos a type instance that supports indexing directly.
To demonstrate using arrays for the member properties (since they already support indexers):
would allow you to say:
我相信接受的答案是错误的。 如果您使用显式接口实现,这是可能的:
那么您可以完全按照您想要的方式使用它:
I believe the accepted answer is wrong. It is possible if you will use explicit interface implementation:
Then you can use it exactly like you wanted:
C# 没有返回类型重载。 如果输入参数不同,您可以定义多个索引器。
C# doesn't have return type overloading. You can define multiple indexers if their input parameters are different.
不,你不能这样做。 只有其签名仅因返回类型而异的方法才是转换运算符。 索引器必须具有不同的输入参数类型才能进行编译。
No you cant do it. Only methods that can have their signatures differ only by return type are conversion operators. Indexers must have different input parameter types to get it to compile.