C# 在列表中存储不同的类型?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9184764/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Storing different types inside a list?
提问by Michael0x2a
Related: A list of multiple data types?
相关:多种数据类型的列表?
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).
上面的问题涵盖了如何使用接口创建一个仅允许用户定义类的列表。但是如果我想要一个只接受双精度和字符串的列表呢?双打和我写的课怎么样?一个只接受一个类 a 和一个其他人编写的类的列表怎么样(所以我不能向 3rd 方类添加接口,我认为)。
I considered using List<object>, but I don't know if that's the accepted best practice.
我考虑过使用List<object>,但我不知道这是否是公认的最佳实践。
回答by Samich
You can specify not only custom types. List<int>, List<double>, List<string>will works as well. If you need to store mixed types - you need to specify closest base class for all types. In List<object>can be stored instance of any type.
您不仅可以指定自定义类型。List<int>, List<double>,List<string>也会起作用。如果您需要存储混合类型 - 您需要为所有类型指定最接近的基类。在List<object>可以存储在任何类型的实例。
回答by Denis Biondic
You can create a custom collection in which you implement Add() method which only accepts doubles and string, something like:
您可以创建一个自定义集合,在其中实现 Add() 方法,该方法只接受双精度和字符串,例如:
void Add(object toAdd)
{
if (toAdd is string)
// add into inner collection ...
... (same for double)
}
But, to be honest, I really can't think of any way that you would need a collection that accepts only these two types. You can probably solve the problem some other way...
但是,老实说,我真的想不出你需要一个只接受这两种类型的集合的任何方式。您可能可以通过其他方式解决问题...
回答by EKet
You should create a class
你应该创建一个类
public class MyClass
{
public string x {get;set;}
public double y{get;set;}
}
Then just create an array of that class. This class can have whatever types you want, that's the beauty of having objects in an object oriented language.
然后只需创建该类的数组。这个类可以有你想要的任何类型,这就是在面向对象语言中拥有对象的美妙之处。
public MyClass[] someList=new MyClass[insert_number_of_elements];
回答by Francisco Soto
回答by Michel Keijzers
You also can use the ? option, make a list of the following type:
您也可以使用 ? 选项,列出以下类型:
public class MyClass
{
public string? x {get;set;}
public double? y {get;set;}
}
This way you can select if none, one or both can have a value.
通过这种方式,您可以选择是否没有、一个或两个都可以有值。
Or if you don't like the HasValue/Value functions:
或者,如果您不喜欢 HasValue/Value 函数:
public class MyClass
{
public enum EType { String, Double };
EType TypeFilled {get; private set }
string _x;
public string X { get { return _x; }; set { _x = value; TypeFilled = EType.String; }
double y;
public double y { get { return _y; }; set { _y = value; TypeFilled = EType.Double; }
}
This way the typeFilled property decides what is filled. You could add validation to prevent being set twice etc.
这样, typeFilled 属性决定填充的内容。您可以添加验证以防止被设置两次等。
回答by Mhan7
I know that I'm a little late to the party, but I do this all the time. Especially when I'm initializing new classes. I like to do the heavy lifting outside of my main method, so I use object arrays to handle it. Object[]lets you put whatever you want into the array. then, when you return the array, you cast the array value to whatever you want. for example:
我知道我参加聚会有点晚了,但我一直这样做。特别是当我初始化新类时。我喜欢在我的主要方法之外做繁重的工作,所以我使用对象数组来处理它。 Object[]让你把任何你想要的东西放到数组中。然后,当您返回数组时,您可以将数组值转换为您想要的任何值。例如:
int NewInt = (int)InitArray[0];
string NewString = (String)InitArray[1];
double NewDouble = (Double)InitArray[2];
That might not be the most elegant solution in all cases, but for the work I do, it certainly handles the single output problem nicely.
这可能不是所有情况下最优雅的解决方案,但对于我所做的工作,它肯定能很好地处理单输出问题。

