Pair Type, maybe even a Trio

I know there is a Pair type inside the Web UI framework, but I feel this idea is too handy not to implement as a generic type.  This will probably have been done sometimes before me it is untrue lol, but any how.  I decided to make the class then and there instead of look for one already made!  Plus it is a simple class the difference in design from others' versions will be minimal.

It has so many uses.  Another option could be a Trio!  Another idea which could be useful is a Range Type.  Throw IEnumerable and IEnumerator into the mix and you get something which, from what I have read, is a type long anticipated in the C# language, which other languages have already provided, with real short implementation.

I am re-inventing the wheel here no doubt, but for anyone who has not yet experienced the benefits, then this post is for you! :-)

 

    public class Pair<T>
    {
        private T m_first;

        private T m_second;

        public Pair(T first, T second)
        {
            m_first = first;
            m_second = second;
        }
        public T First
        {
            get { return m_first; }
            set { m_first = value; }
        }

        public T Second
        {
            get { return m_second; }
            set { m_second = value; }
        }
    }

 

Implementation Example

    public class Program
    {
        static void Main(string[] args)
        {
            Pair<float> p = new Pair<float>(5, 10);
            Console.WriteLine(p.First);
            Console.WriteLine(p.Second);
            Console.ReadLine();
        }
    }

Output

image

 

Maybe a Trio

 

    public class Trio<T>
    {
        private T m_first;

        private T m_second;

        private T m_third;

        public Trio(T first, T second, T third)
        {
            m_first = first;
            m_second = second;
            m_third = third;
        }
        public T First
        {
            get { return m_first; }
            set { m_first = value; }
        }

        public T Second
        {
            get { return m_second; }
            set { m_second = value; }
        }

        public T Third
        {
            get { return m_third; }
            set { m_third = value; }
        }
    }
 

Cheers

Andrew :-)

 

Update made (on the same day as this post lol)

I have made an update to the Pair class.  I suppose you could call it a different class to be honest, but here it is any.  I have allowed for the definition of two different types for the pair. 

Hide Code [-]
    public class Pair<T, S>
    {
        private T m_first;

        private S m_second;

        public Pair(T first, S second)
        {
            m_first = first;
            m_second = second;
        }
        public T First
        {
            get { return m_first; }
            set { m_first = value; }
        }

        public S Second
        {
            get { return m_second; }
            set { m_second = value; }
        }
    }
{..} Click Show Code
Published Thursday, April 24, 2008 10:26 AM by REA_ANDREW

Comments

# re: Pair Type, maybe even a Trio

Sunday, September 27, 2009 6:08 AM by Fredde

Check out the built-in KeyValuePair (System.Collections.Generic.KeyValuePair) too, it works like the updated version.

Leave a Comment

(required) 
(required) 
(optional)
(required)