关于“this”的基本 C# 类问题关键词
我正在查看别人的代码,我看到了这样的说法:
public CustomClassName this [ string varName]
请原谅这个问题的新颖性,但方括号让我失望。 这是方法还是构造函数?
在这种情况下“this”变量如何工作?
I'm going through someone else's code and I saw this statement:
public CustomClassName this [ string varName]
Please excuse the newbness of this question, but the square brackets threw me off.
Is this a method or constructor?
How does the "this" variable work in this case?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
它称为索引器。 MSDN 页面。
It's called an indexer. MSDN page.
两者都不是,它是一个索引器。它允许您执行 CustomClassName[ obj ] 并从对象中检索值。
Neither, it's an Indexer. It allows you to do CustomClassName[ obj ] and retrieve a value from the object.
它是一个索引器,因此您可以像访问数组一样访问该类。
It is an indexer, so you can access the class similar to an array.
它正在为您的类型定义一个索引运算符。以
List
类为例。库设计者希望您能够编写如下代码:完成此操作的语法就是您上面发布的内容。因此,对于您自己的类型,您可以...
It is defining an index operator on your type. Take for example, the
List<T>
class. The library designers wanted you to be able to write code like this:The syntax that accomplishes that is what you have posted above. So, for your own types, you can...
这只是 C# 中方括号运算符的重载。
请参阅此处:
如何重载方括号运算符在 C# 中?
this is simply an overload of the square-bracket operator in C#.
see here:
How do I overload the square-bracket operator in C#?