在列表中存储不同的类型? [英] Storing different types inside a list?

查看:48
本文介绍了在列表中存储不同的类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

相关:多种数据类型的列表?

我想知道如何在数组中存储不同的数组类型(包括系统类型)。

I want to know how to store different array types (including system types) inside an array.

上述问题涵盖了如何通过使用接口创建仅允许用户定义的类的列表。但是,如果我想要一个只接受双精度和字符串的列表怎么办?那我写的双打和一堂课呢?那么只接受一个所写的类和其他人所写的类的列表呢(我想我不能将接口添加到第三方类)。

The above question covered how to create a list that will allow only user-defined classes by using interfaces. But what if I want a list that will accept only doubles and strings? What about doubles and a class I wrote? What about a list that will accept only a class a wrote and a class someone else wrote (so I can't add an interface to the 3rd party class, I think).

我考虑过使用 List< object> ,但是我不知道这是否是公认的最佳实践。

I considered using List<object>, but I don't know if that's the accepted best practice.

推荐答案

如果您愿意放弃类型安全性,则可以使用 ArrayList ,这是使用常规药品清单的唯一方法。

If you are willing to give away type safety you can use an ArrayList, which was the only way to use a list of stuff pre generics.

您可以放入一些包装器代码只能接受双打和字符串。

You can put some wrapper code around it to only accept doubles and strings.

这篇关于在列表中存储不同的类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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