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

前端 未结 16 2255
时光说笑
时光说笑 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:26

    Aye, there's System.Web.UI.Pair and System.Web.UI.Triplet (which has an overloaded creator for Pair-type behaviour!)

    0 讨论(0)
  • 2021-02-07 03:27

    Fast-forward to 2010, .NET 4.0 now supports n-tuples of arbitrary n. These tuples implement structural equality and comparison as expected.

    0 讨论(0)
  • 2021-02-07 03:28

    One simple solution has no been mentioned yet. You can also just use a List<T>. It's built in, efficient and easy to use. Granted, it looks a bit weird at first, but it does its job perfectly, especially for a larger count of elements.

    0 讨论(0)
  • 2021-02-07 03:32

    There are not built-in classes for that. You can use KeyValuePair or roll out your own implementation.

    0 讨论(0)
  • 2021-02-07 03:33

    You could use System.Collections.Generic.KeyValuePair as your Pair implementation.

    Or you could just implement your own, they aren't hard:

    public class Triple<T, U, V>
    {
      public T First {get;set;}
      public U Second {get;set;}
      public V Third {get;set;}
    }
    

    Of course, you may someday run into a problem that Triple(string, int, int) is not compatible with Triple(int, int, string). Maybe go with System.Xml.Linq.XElement instead.

    0 讨论(0)
  • 2021-02-07 03:34

    There aren't built ins, but a Pair<T,R> class is trivial to create.

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