Multiple parameters in a List. How to create without a class?

后端 未结 10 1993
不知归路
不知归路 2020-12-12 23:25

This is probably a pretty obvious question, but how would I go about creating a List that has multiple parameters without creating a class.

Example:

相关标签:
10条回答
  • 2020-12-13 00:04

    As said by Scott Chamberlain(and several others), Tuples work best if you don't mind having immutable(ie read-only) objects.

    If, like suggested by David, you want to reference the int by the string value, for example, you should use a dictionary

    Dictionary<string, int> d = new Dictionary<string, int>();
    d.Add("string", 1);
    Console.WriteLine(d["string"]);//prints 1
    

    If, however, you want to store your elements mutably in a list, and don't want to use a dictionary-style referencing system, then your best bet(ie only real solution right now) would be to use KeyValuePair, which is essentially std::pair for C#:

    var kvp=new KeyValuePair<int, string>(2, "a");
    //kvp.Key=2 and a.Value="a";
    kvp.Key = 3;//both key and
    kvp.Value = "b";//value are mutable
    

    Of course, this is stackable, so if you need a larger tuple(like if you needed 4 elements) you just stack it. Granted this gets ugly really fast:

         var quad=new KeyValuePair<KeyValuePair<int,string>, KeyValuePair<int,string>>
                    (new KeyValuePair<int,string>(3,"a"),
                    new KeyValuePair<int,string>(4,"b"));
    //quad.Key.Key=3
    

    So obviously if you were to do this, you should probably also define an auxiliary function.

    My advice is that if your tuple contains more than 2 elements, define your own class. You could use a typedef-esque using statement like :

    using quad = KeyValuePair<KeyValuePair<int,string>, KeyValuePair<int,string>>;
    

    but that doesn't make your instantiations any easier. You'd probably spend a lot less time writing template parameters and more time on the non-boilerplate code if you go with a user-defined class when working with tuples of more than 2 elements

    0 讨论(0)
  • 2020-12-13 00:04

    This works fine with me

    List<string> myList = new List<string>();
    myList.Add(string.Format("{0}|{1}","hello","1") ;
    
    
    label:myList[0].split('|')[0] 
    val:  myList[0].split('|')[1]
    
    0 讨论(0)
  • 2020-12-13 00:05

    List only accepts one type parameter. The closest you'll get with List is:

     var list = new List<Tuple<string, int>>();
     list.Add(Tuple.Create("hello", 1));
    
    0 讨论(0)
  • 2020-12-13 00:07

    To add to what other suggested I like the following construct to avoid the annoyance of adding members to keyvaluepair collections.

    public class KeyValuePairList<Tkey,TValue> : List<KeyValuePair<Tkey,TValue>>{
        public void Add(Tkey key, TValue value){
            base.Add(new KeyValuePair<Tkey, TValue>(key, value));
        }
    }
    

    What this means is that the constructor can be initialized with better syntax::

    var myList = new KeyValuePairList<int,string>{{1,"one"},{2,"two"},{3,"three"}};
    

    I personally like the above code over the more verbose examples Unfortunately C# does not really support tuple types natively so this little hack works wonders.

    If you find yourself really needing more than 2, I suggest creating abstractions against the tuple type.(although Tuple is a class not a struct like KeyValuePair this is an interesting distinction).

    Curiously enough, the initializer list syntax is available on any IEnumerable and it allows you to use any Add method, even those not actually enumerable by your object. It's pretty handy to allow things like adding an object[] member as a params object[] member.

    0 讨论(0)
  • 2020-12-13 00:08

    If appropriate, you might use a Dictionary which is also a generic collection:

    Dictionary<string, int> d = new Dictionary<string, int>();
    d.Add("string", 1);
    
    0 讨论(0)
  • 2020-12-13 00:20

    For those wanting to use a Class.

    Create a Class with all the parameters you want

    Create a list with the class as parameter

    class MyClass
            {
                public string S1;
                public string S2;
            }
    
    List<MyClass> MyList= new List<MyClass>();

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