What is C# analog of C++ std::pair?

前端 未结 14 1943
情话喂你
情话喂你 2020-11-29 16:54

I\'m interested: What is C#\'s analog of std::pair in C++? I found System.Web.UI.Pair class, but I\'d prefer something template-based.

Than

相关标签:
14条回答
  • 2020-11-29 17:14

    Tuples are available since .NET4.0 and support generics:

    Tuple<string, int> t = new Tuple<string, int>("Hello", 4);
    

    In previous versions you can use System.Collections.Generic.KeyValuePair<K, V> or a solution like the following:

    public class Pair<T, U> {
        public Pair() {
        }
    
        public Pair(T first, U second) {
            this.First = first;
            this.Second = second;
        }
    
        public T First { get; set; }
        public U Second { get; set; }
    };
    

    And use it like this:

    Pair<String, int> pair = new Pair<String, int>("test", 2);
    Console.WriteLine(pair.First);
    Console.WriteLine(pair.Second);
    

    This outputs:

    test
    2
    

    Or even this chained pairs:

    Pair<Pair<String, int>, bool> pair = new Pair<Pair<String, int>, bool>();
    pair.First = new Pair<String, int>();
    pair.First.First = "test";
    pair.First.Second = 12;
    pair.Second = true;
    
    Console.WriteLine(pair.First.First);
    Console.WriteLine(pair.First.Second);
    Console.WriteLine(pair.Second);
    

    That outputs:

    test
    12
    true
    
    0 讨论(0)
  • 2020-11-29 17:16

    Depending on what you want to accomplish, you might want to try out KeyValuePair.

    The fact that you cannot change the key of an entry can of course be rectified by simply replacing the entire entry by a new instance of KeyValuePair.

    0 讨论(0)
  • 2020-11-29 17:18

    System.Web.UI contained the Pair class because it was used heavily in ASP.NET 1.1 as an internal ViewState structure.

    Update Aug 2017: C# 7.0 / .NET Framework 4.7 provides a syntax to declare a Tuple with named items using the System.ValueTuple struct.

    //explicit Item typing
    (string Message, int SomeNumber) t = ("Hello", 4);
    //or using implicit typing 
    var t = (Message:"Hello", SomeNumber:4);
    
    Console.WriteLine("{0} {1}", t.Message, t.SomeNumber);
    

    see MSDN for more syntax examples.

    Update Jun 2012: Tuples have been a part of .NET since version 4.0.

    Here is an earlier article describing inclusion in.NET4.0 and support for generics:

    Tuple<string, int> t = new Tuple<string, int>("Hello", 4);
    
    0 讨论(0)
  • 2020-11-29 17:18

    I created a C# implementation of Tuples, which solves the problem generically for between two and five values - here's the blog post, which contains a link to the source.

    0 讨论(0)
  • 2020-11-29 17:20

    I typically extend the Tuple class into my own generic wrapper as follows:

    public class Statistic<T> : Tuple<string, T>
    {
        public Statistic(string name, T value) : base(name, value) { }
        public string Name { get { return this.Item1; } }
        public T Value { get { return this.Item2; } }
    }
    

    and use it like so:

    public class StatSummary{
          public Statistic<double> NetProfit { get; set; }
          public Statistic<int> NumberOfTrades { get; set; }
    
          public StatSummary(double totalNetProfit, int numberOfTrades)
          {
              this.TotalNetProfit = new Statistic<double>("Total Net Profit", totalNetProfit);
              this.NumberOfTrades = new Statistic<int>("Number of Trades", numberOfTrades);
          }
    }
    
    StatSummary summary = new StatSummary(750.50, 30);
    Console.WriteLine("Name: " + summary.NetProfit.Name + "    Value: " + summary.NetProfit.Value);
    Console.WriteLine("Name: " + summary.NumberOfTrades.Value + "    Value: " + summary.NumberOfTrades.Value);
    
    0 讨论(0)
  • 2020-11-29 17:21

    Since .NET 4.0 you have System.Tuple<T1, T2> class:

    // pair is implicitly typed local variable (method scope)
    var pair = System.Tuple.Create("Current century", 21);
    
    0 讨论(0)
提交回复
热议问题