Michael0x2a
Michael0x2a

Reputation: 63978

Storing different types inside a list?

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).

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

Upvotes: 19

Views: 95032

Answers (7)

PizzaLvr49
PizzaLvr49

Reputation: 11

You can use List<dynamic>() but this is not type-safe and could produce an error at runtime. For more info dynamic type c#

Upvotes: 0

Mhan7
Mhan7

Reputation: 11

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:

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.

Upvotes: 0

Michel Keijzers
Michel Keijzers

Reputation: 15347

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:

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.

Upvotes: 3

Francisco Soto
Francisco Soto

Reputation: 10392

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.

Upvotes: 5

EKet
EKet

Reputation: 7314

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];

Upvotes: 3

Denis Biondic
Denis Biondic

Reputation: 8201

You can create a custom collection in which you implement Add() method which only accepts doubles and string, something like:

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...

Upvotes: 2

Samich
Samich

Reputation: 30095

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.

Upvotes: 9

Related Questions