且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在C#中将对象分配给hastable值

更新时间:2021-12-07 22:13:52

这段代码毫无意义;您添加了像8533这样的硬编码的立即常量,错过了类型转换,依此类推.你不懂强打字.您可以键入objhashmap[key]:
This code makes little sense; you add hard-coded immediate constant like 8533, miss the type cast, and so on. You don''t understand strong typing. You could type-cast objhashmap[key]:
ADXVoice obj = (ADXVoice)objhashmap[key];


如果运行时类型与要转换为的类型赋值兼容,则可以这样做.在您的代码示例中,您显示了添加类型ADXVoice的值,因此对于该值,类型转换将成功.但是为什么要这样做?

不要使用那些过时的非通用(非专用)集合类型.使用例如System.Collections.Generic.Dictionary<int, ADXVoice>:
http://msdn.microsoft.com/en-us/library/xfhwa508%28v = vs.110%29.aspx [ ^ ].

您需要学习继承,编译时类型运行时类型以及其他基本的OOP思想以及泛型.

—SA


which is possible if runtime type is assignment-compatible with the type to be cast to. In your code sample, you show adding the value of the type ADXVoice, so, for this value, the typecast will be successful. But why doing so?

Don''t use those obsolete non-generic (non-specialized) collection types. Use, for example, System.Collections.Generic.Dictionary<int, ADXVoice>:
http://msdn.microsoft.com/en-us/library/xfhwa508%28v=vs.110%29.aspx[^].

You need to learn inheritance, compile-time types vs runtime types and other basic OOP ideas, as well as generics.

—SA