Storing different types inside a list?

后端 未结 6 951
广开言路
广开言路 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:41

    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];
    
    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2021-02-19 03:48

    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.

    0 讨论(0)
  • 2021-02-19 03:49

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

    0 讨论(0)
  • 2021-02-19 03:55

    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.

    0 讨论(0)
  • 2021-02-19 03:59

    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.

    0 讨论(0)
提交回复
热议问题