我很了解 C#,但这对我来说很奇怪。在一些旧程序中,我看到过这样的代码:
public MyType this[string name]
{
......some code that finally return instance of MyType
}
怎么称呼?这有什么用呢?
It is indexer http://msdn.microsoft.com/en-us/library/6x16t2tx.aspx。声明后,您可以这样做:
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;
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)