Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. What would the c# equivalent of make_pair be

What would the c# equivalent of make_pair be

Scheduled Pinned Locked Moved C#
c++csharptutorialquestion
6 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    PaleyX
    wrote on last edited by
    #1

    In C++\STL i can create pairs of values using make_pair so I could, for example, look up a value in a map/dictionary using a 2 value key: XXX::iterator it = m_map.find(make_pair(x,y)); Is there an equivalent in C# or if not a way around it? TIA

    H N 2 Replies Last reply
    0
    • P PaleyX

      In C++\STL i can create pairs of values using make_pair so I could, for example, look up a value in a map/dictionary using a 2 value key: XXX::iterator it = m_map.find(make_pair(x,y)); Is there an equivalent in C# or if not a way around it? TIA

      H Offline
      H Offline
      Heath Stewart
      wrote on last edited by
      #2

      A Hashtable stores key/values pairs that are both objects, meaning that you can store anything (since every Type in .NET extends System.Object). This means that you can store, for example, a class (best to use reference types to avoid boxing and unboxing performance penalties) with a couple of properties like so:

      public class Person
      {
      string name, DateTime birthday;
      public string Name
      {
      get { return name; }
      set { if (value == null) throw new ArgumentNullException(); name = value; }
      }
      public DateTime Birthday
      {
      get { return birthday; }
      set { birthday = value; }
      }
      }

      Then create an IComparer implementation to compare whatever property or properties you want, or have your class implement IComparable so that the default Comparer can juse use your implementation (this option is often best when defining your own types as it works without implementing and using a separate Type). If you read the documentation for the Hashtable, however, you'll notice that if you don't pass an IComparer to the constructor the default comparer it uses will use the Equals override. So, you could also override Object.Equals (which implies you should override Object.GetHashCode) and not pass a comparer, or implement IComparable on your class and pass Comparer.Default to the Hashtable constructor, or implement IComparer and pass your implementation to the Hashtable constructor. This will allow you to compare keys, not values, though. For that, enumerate the Hashtable.Values collection or enumerate (using the IDictionaryEnumerator) the entire Hashtable to compare values so that you have both the key and value. As far as whether to override Equals or implement IComparable, you could do both. This posting is provided "AS IS" with no warranties, and confers no rights. Software Design Engineer Developer Division Sustained Engineering Microsoft [My Articles] [My Blog]

      P 1 Reply Last reply
      0
      • P PaleyX

        In C++\STL i can create pairs of values using make_pair so I could, for example, look up a value in a map/dictionary using a 2 value key: XXX::iterator it = m_map.find(make_pair(x,y)); Is there an equivalent in C# or if not a way around it? TIA

        N Offline
        N Offline
        Nick Parker
        wrote on last edited by
        #3

        You might look into classes that implement IDictionary, such as HybridDictionary[^]. The method access will be different but you will find what you need for version 1.1 of the framework to implement it. With version 2.0 of the .NET Framework you can do something as simple as the following with generics:

        public class pair<T1, T2>
        {
            public T1 First
            {
                get {return this.t1;}
                set {this.t1 = value;}
            }
        
            public T2 Second
            {
                get { return this.t2; }
                set { this.t2 = value; }
            }
        
            public pair(T1 type1, T2 type2)
            {
                this.t1 = type1;
                this.t2 = type2;
            }
            private T1 t1;
            private T2 t2;
        }
        /// Implementation
        private void button1\_Click(object sender, EventArgs e)
        {
            pair<int, int> i = new pair<int, int>(5, 10);
            MessageBox.Show(string.Format("First value:{0}, second value:{1}", 
                i.First.ToString(), i.Second.ToString()));
        }
        

        - Nick Parker
        My Blog | My Articles

        L 1 Reply Last reply
        0
        • H Heath Stewart

          A Hashtable stores key/values pairs that are both objects, meaning that you can store anything (since every Type in .NET extends System.Object). This means that you can store, for example, a class (best to use reference types to avoid boxing and unboxing performance penalties) with a couple of properties like so:

          public class Person
          {
          string name, DateTime birthday;
          public string Name
          {
          get { return name; }
          set { if (value == null) throw new ArgumentNullException(); name = value; }
          }
          public DateTime Birthday
          {
          get { return birthday; }
          set { birthday = value; }
          }
          }

          Then create an IComparer implementation to compare whatever property or properties you want, or have your class implement IComparable so that the default Comparer can juse use your implementation (this option is often best when defining your own types as it works without implementing and using a separate Type). If you read the documentation for the Hashtable, however, you'll notice that if you don't pass an IComparer to the constructor the default comparer it uses will use the Equals override. So, you could also override Object.Equals (which implies you should override Object.GetHashCode) and not pass a comparer, or implement IComparable on your class and pass Comparer.Default to the Hashtable constructor, or implement IComparer and pass your implementation to the Hashtable constructor. This will allow you to compare keys, not values, though. For that, enumerate the Hashtable.Values collection or enumerate (using the IDictionaryEnumerator) the entire Hashtable to compare values so that you have both the key and value. As far as whether to override Equals or implement IComparable, you could do both. This posting is provided "AS IS" with no warranties, and confers no rights. Software Design Engineer Developer Division Sustained Engineering Microsoft [My Articles] [My Blog]

          P Offline
          P Offline
          PaleyX
          wrote on last edited by
          #4

          Thanks - I was looking at Hashtables when I got your reply, your explanation is certainly more approachable than the stuff I was reading :doh: Rugby League: The Greatest Game Of All.

          1 Reply Last reply
          0
          • N Nick Parker

            You might look into classes that implement IDictionary, such as HybridDictionary[^]. The method access will be different but you will find what you need for version 1.1 of the framework to implement it. With version 2.0 of the .NET Framework you can do something as simple as the following with generics:

            public class pair<T1, T2>
            {
                public T1 First
                {
                    get {return this.t1;}
                    set {this.t1 = value;}
                }
            
                public T2 Second
                {
                    get { return this.t2; }
                    set { this.t2 = value; }
                }
            
                public pair(T1 type1, T2 type2)
                {
                    this.t1 = type1;
                    this.t2 = type2;
                }
                private T1 t1;
                private T2 t2;
            }
            /// Implementation
            private void button1\_Click(object sender, EventArgs e)
            {
                pair<int, int> i = new pair<int, int>(5, 10);
                MessageBox.Show(string.Format("First value:{0}, second value:{1}", 
                    i.First.ToString(), i.Second.ToString()));
            }
            

            - Nick Parker
            My Blog | My Articles

            L Offline
            L Offline
            leppie
            wrote on last edited by
            #5

            You will have to use a struct or override GetHashCode()/Equals() else it wont work for key of a hashtable :p top secret
            Download xacc-ide 0.0.3 now!
            See some screenshots

            N 1 Reply Last reply
            0
            • L leppie

              You will have to use a struct or override GetHashCode()/Equals() else it wont work for key of a hashtable :p top secret
              Download xacc-ide 0.0.3 now!
              See some screenshots

              N Offline
              N Offline
              Nick Parker
              wrote on last edited by
              #6

              It works much better when you use generics though. ;-) - Nick Parker
              My Blog | My Articles

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups