c# - 方法声明中的方括号如何适合 c#?
方法声明中的方括号如何适合 C#?这是我在阅读 WPF 验证时看到的,可以使用 IDataErrorInfo 和以下示例。
public string this[string propertyName]
// Error handling takes place here.
public string this[string propertyName] // <== IE HERE
{
get
// etc
}
}
我注意到 MSDN 说“方括号 ([]) 用于数组、索引器和属性。它们也可以与指针一起使用。”那么上面的用法是指针吗?
How do square brackets in a method declaration fit in with c#? That is I see when reading up on WPF validation one can use IDataErrorInfo with an example of the following.
public string this[string propertyName]
// Error handling takes place here.
public string this[string propertyName] // <== IE HERE
{
get
// etc
}
}
I note MSDN says "Square brackets ([]) are used for arrays, indexers, and attributes. They can also be used with pointers." So is the above usage a pointer?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
那将是一个索引器属性。它们对于自定义集合很有用:
然后您可以使用该集合,例如:
That would be an indexer property. They're useful on custom collections:
And then you can use the collection like:
这是索引器的声明。它类似于数组索引。
propertyName
是一个字符串,该方法使用它来索引某种集合。该方法从集合中返回相应的字符串。当然,该方法可以做其他事情,但这就是语义的含义。
This is a declaration of an Indexer. It's analagous to array indexing.
propertyName
is a string which the method uses to index into some kind of collection. The method returns the corresponding string from the collection.Of course, the method could do something else, but that's what the semantics mean.
这是 C# 语言的一项标准功能,称为索引器。通常,您在编写自己的集合或类似类型时会使用它们。这是一个简短的(非现实世界)示例。
如果您有创造力,您可以使用索引器做很多很酷的事情,这是该语言的一个非常巧妙的功能。
This is a standard feature of the C# language called an Indexer. Generally you would use these when writing your own collections, or similar types. Here is a brief (not real world) example.
There's a lot of cool things you can use indexers for if you are creative, it's a really neat feature of the language.