Storing different types inside a list?

后端 未结 6 1047
广开言路
广开言路 2021-02-19 03:34

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 ho

6条回答
  •  孤街浪徒
    2021-02-19 03:47

    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.

提交回复
热议问题