这个关键字作为属性 [英] this keyword as a property
本文介绍了这个关键字作为属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我知道C#很好,但它是一些奇怪的事情对我来说。
在一些旧的程序,我看到这个代码:
I know c# well, but it is something strange for me. In some old program, I have seen this code:
public MyType this[string name]
{
......some code that finally return instance of MyType
}
如何它叫什么名字?有什么用这个?
How it is called? What is the use of this?
推荐答案
这是的索引。当你宣布它,你可以这样做:
It is indexer. After you declared it you can do like this:
class MyClass
{
Dictionary<string, MyType> collection;
public MyType this[string name]
{
get { return collection[name]; }
set { collection[name] = value; }
}
}
// Getting data from indexer.
MyClass myClass = ...
MyType myType = myClass["myKey"];
// Setting data with indexer.
MyType anotherMyType = ...
myClass["myAnotherKey"] = anotherMyType;
这篇关于这个关键字作为属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文