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

前端 未结 14 1944
情话喂你
情话喂你 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:35

    Some answers seem just wrong,

    1. you can't use dictionary how would store the pairs (a,b) and (a,c). Pairs concept should not be confused with associative look up of key and values
    2. lot of the above code seems suspect

    Here is my pair class

    public class Pair<X, Y>
    {
        private X _x;
        private Y _y;
    
        public Pair(X first, Y second)
        {
            _x = first;
            _y = second;
        }
    
        public X first { get { return _x; } }
    
        public Y second { get { return _y; } }
    
        public override bool Equals(object obj)
        {
            if (obj == null)
                return false;
            if (obj == this)
                return true;
            Pair<X, Y> other = obj as Pair<X, Y>;
            if (other == null)
                return false;
    
            return
                (((first == null) && (other.first == null))
                    || ((first != null) && first.Equals(other.first)))
                  &&
                (((second == null) && (other.second == null))
                    || ((second != null) && second.Equals(other.second)));
        }
    
        public override int GetHashCode()
        {
            int hashcode = 0;
            if (first != null)
                hashcode += first.GetHashCode();
            if (second != null)
                hashcode += second.GetHashCode();
    
            return hashcode;
        }
    }
    

    Here is some test code:

    [TestClass]
    public class PairTest
    {
        [TestMethod]
        public void pairTest()
        {
            string s = "abc";
            Pair<int, string> foo = new Pair<int, string>(10, s);
            Pair<int, string> bar = new Pair<int, string>(10, s);
            Pair<int, string> qux = new Pair<int, string>(20, s);
            Pair<int, int> aaa = new Pair<int, int>(10, 20);
    
            Assert.IsTrue(10 == foo.first);
            Assert.AreEqual(s, foo.second);
            Assert.AreEqual(foo, bar);
            Assert.IsTrue(foo.GetHashCode() == bar.GetHashCode());
            Assert.IsFalse(foo.Equals(qux));
            Assert.IsFalse(foo.Equals(null));
            Assert.IsFalse(foo.Equals(aaa));
    
            Pair<string, string> s1 = new Pair<string, string>("a", "b");
            Pair<string, string> s2 = new Pair<string, string>(null, "b");
            Pair<string, string> s3 = new Pair<string, string>("a", null);
            Pair<string, string> s4 = new Pair<string, string>(null, null);
            Assert.IsFalse(s1.Equals(s2));
            Assert.IsFalse(s1.Equals(s3));
            Assert.IsFalse(s1.Equals(s4));
            Assert.IsFalse(s2.Equals(s1));
            Assert.IsFalse(s3.Equals(s1));
            Assert.IsFalse(s2.Equals(s3));
            Assert.IsFalse(s4.Equals(s1));
            Assert.IsFalse(s1.Equals(s4));
        }
    }
    
    0 讨论(0)
  • 2020-11-29 17:35

    If it's about dictionaries and the like, you're looking for System.Collections.Generic.KeyValuePair<TKey, TValue>.

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