强类型集合 [英] Strong Typed Collections

查看:72
本文介绍了强类型集合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望我的收藏品包含特定类型的对象。大多数

我读到的主题涉及创建一个自定义集合类

,它扩展了DictionaryBase或IEnumerable。但这意味着对于我关心的每个

对象类型,我必须创建一个自定义集合类。

难道没有更简单的方式来说我想要这个集合包含

只有X类的对象"?


谢谢

I''d like my collections to contain specific types of objects. Most of
the topics i read on this involves creating a custom collection class
that extends DictionaryBase or IEnumerable. But this means for every
object type i care about, i must create a custom collection class for.
Isn''t there a simpler way to say "i want this collection to contain
only objects of Class X"?

thanks

推荐答案

ok ...我看到我可以使用system.collections.generic命名空间来支持
这个。在我的情况下,我想使用键/值对,所以IDictionary< int,

MyClass>是我用的。问题是,如何将哈希表投射到

这个?

如果不能这样做,我怎样才能实例化并向

集合声明为IDictionary< int,MyCalss> ;.


另外,有没有办法为IDictionary< int,

MyClass>声明类型或别名,所以我不需要每次都需要输入那个声明

声明该特定集合的变量?

ok...i see that i can use system.collections.generic namespace for
this. in my case, i want to use a key/value pair so IDictionary< int,
MyClass > is what i use. The problem is, how do i cast a hashtable to
this?
If that can''t be done, how can i instantiate and add values to a
collection declared as IDictionary< int, MyCalss>.

Also, is there a way to declare a type or alias for "IDictionary<int,
MyClass>", so i don''t have to type that declaration everytime i need to
declare a variable of that particular collection?


farseer写道:
farseer wrote:
ok ...我看到我可以使用system.collections.generic命名空间来实现这个目的。在我的情况下,我想使用键/值对,所以IDictionary< int,
MyClass>是我用的。问题是,我如何将哈希表投射到这个?


你没有。你宁愿使用Dictionary,它是Hashtable的强类型

变体。

另外,有没有办法为IDictionary<声明一个类型或别名? int,
MyClass>",所以每次我需要声明该特定集合的变量时,我不必输入该声明吗?
ok...i see that i can use system.collections.generic namespace for
this. in my case, i want to use a key/value pair so IDictionary< int,
MyClass > is what i use. The problem is, how do i cast a hashtable to
this?
You don''t. You''d rather use Dictionary, which is the strongly typed
variant of Hashtable.
Also, is there a way to declare a type or alias for "IDictionary<int,
MyClass>", so i don''t have to type that declaration everytime i need
to declare a variable of that particular collection?




在命名空间范围内(我希望我的语法正确):


使用IMyClassDictionary = IDictionary< int,MyClass> ;;


问候,


-

Andreas Huber


在通过私人电子邮件回复时,请删除垃圾邮件并从标题中显示的地址陷阱





At namespace scope (I hope I get the syntax right):

using IMyClassDictionary = IDictionary<int,MyClass>;

Regards,

--
Andreas Huber

When replying by private email, please remove the words spam and trap
from the address shown in the header.


非常感谢,这就是我想要的。

Thanks much, that''s what i was looking for.


这篇关于强类型集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆