c# – 这个关键字作为一个属性
发布时间:2020-12-15 03:57:19 所属栏目:百科 来源:网络整理
导读:我知道c#好,但对我而言奇怪. 在一些旧的程序中,我看到这个代码: public MyType this[string name]{ ......some code that finally return instance of MyType} 怎么叫这是什么用途? 解决方法 这是 indexer.你宣布后,你可以这样做: class MyClass{ Diction
我知道c#好,但对我而言奇怪.
在一些旧的程序中,我看到这个代码: public MyType this[string name] { ......some code that finally return instance of MyType } 怎么叫这是什么用途? 解决方法
这是
indexer.你宣布后,你可以这样做:
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; (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |