What's the best way of using a pair (triple, etc) of values as one value in C#?

前端 未结 16 2570
时光说笑
时光说笑 2021-02-07 03:00

That is, I\'d like to have a tuple of values.

The use case on my mind:

Dictionary, object>

or

<         


        
16条回答
  •  野趣味
    野趣味 (楼主)
    2021-02-07 03:38

    KeyValuePair is the best class to extend if you don't want to create your own classes.

    int id = 33;
    string description = "This is a custom solution";
    DateTime created = DateTime.Now;
    
    KeyValuePair> triple =
       new KeyValuePair>();
    triple.Key = id;
    triple.Value.Key = description;
    triple.Value.Value = created;
    

    You can extend it to as many levels as you want.

    KeyValuePair, string, string> quadruple =
       new KeyValuePair, string, string>();
    

    Note: The classes Triplet and Pair exists inside the System.Web-dll, so it's not very suitable for other solutions than ASP.NET.

提交回复
热议问题