Martin Bodocky

No less, no more. Just live between code.

Generic Pair C#


When you are SharePoint developer you don’t have functionality from .Net 4 such
Tuple object.
Presumably, you are not happy as me with it. I designed my own pair object with
using and I’m happy to share that.

 

///<summary>

/// CLass cover functionality as related two values with different or same type

///</summary>

///<typeparam name=”T”>Type of first value</typeparam>

///<typeparam name=”U”>Type of second value</typeparam>

[Serializable]

public class Pair<T,U>

{

#region
Properties

public T First { get; set; }

public U Second { get; set; }

#endregion

 

#region Ctors

///<summary>

/// Empty Constructor

///</summary>

public Pair() { }

///<summary>

/// Public building constructor

///</summary>

///<param name=”first”>First value</param>

///<param name=”second”>Second value</param>

public Pair(T first, U second)

{

First = first;

Second = second;

}

#endregion

#region
Override methods

///<summary>

/// Generate Object Hash code

///</summary>

///<returns>hash code</returns>

public override int GetHashCode()

{

return (First != null ? First.GetHashCode() : 0) + 29 * (Second != null ? Second.GetHashCode() : 0);

}

 

///<summary>

/// Generate string representation

///</summary>

///<returns></returns>

public override string ToString()

{

return string.Format(“First: {0}, Second: {1}”, this.First,  this.Second);

}

#endregion

 

#region
Static methods

///<summary>

/// Convert dictionary to list of paired objects

///</summary>

///<param name=”dictionary”>Input directory</param>

///<returns>List of pairs</returns>

public static List<Pair<T, U>> DictionaryToList(IDictionary<T,U> dictionary)

{

List<Pair<T, U>> list = new List<Pair<T,U>>();

if (dictionary != null)

{

foreach (KeyValuePair<T, U> pair in dictionary)

    list.Add(new Pair<T, U>(pair.Key, pair.Value));

}

return list;

}

///<summary>

/// Convert list of pairs to dictionary

///</summary>

///<param name=”list”>Input lis of pairs</param>

///<returns>dictionary of pairs objects</returns>

public static Dictionary<T,U> ListToDictionary(IList<Pair<T,U>> list)

{

Dictionary<T,U> dictionary = new Dictionary<T, U>();

try

{

if (list != null)

{

foreach(Pair<T, U> pair in list)

  dictionary.Add(pair.First, pair.Second);

}

}

catch (ArgumentException ex)

{

throw new Exception(“If key’s value already exists, use fucntion ListToDictionaryDimension.”, ex);

}

return dictionary;

}

///<summary>

/// Convert list of pairs to dictionary avoiding duplicates with key values

///</summary>

///<param name=”list”>List of pairs</param>

///<returns>Dictionary of pairs</returns>

public static Dictionary<T,List<U>> ListToDictionaryDimension(IList<Pair<T,U>> list)

{

Dictionary<T,List<U>> dictionary = new Dictionary<T, List<U>>();

if (list != null)

{

foreach(Pair<T, U> pair in list)

{

List<U> listOfValues = null;

if (!dictionary.TryGetValue(pair.First,out listOfValues))

{

listOfValues = new List<U>();

dictionary.Add(pair.First, listOfValues);

}

listOfValues.Add(pair.Second);

}

}

return dictionary;

}

///<summary>

/// Compare operator

///</summary>

///<param name=”p1″>First pair</param>

///<param name=”p2″>Second operator</param>

///<returns>result of comaprision</returns>

public static bool operator ==(Pair<T, U> p1, Pair<T, U> p2)

{

if (Equals(p1, null) && Equals(p2, null)) return true;

if (Equals(p1, null) || Equals(p2, null)) return false;

return Equals(p1.First, p2.First) && Equals(p1.Second, p2.Second);

}

///<summary>

/// Inverse compare operator

///</summary>

///<param name=”p1″>First pair</param>

///<param name=”p2″>Second operator</param>

///<returns>result of comaprision</returns>

public static bool operator !=(Pair<T, U> p1, Pair<T, U> p2)

{

if (Equals(p1, null) && Equals(p2, null)) return true;

if (Equals(p1, null) || Equals(p2, null)) return false;

return !(Equals(p1.First, p2.First) && Equals(p1.Second, p2.Second));

}

#endregion

}

 

And using:

 

Pair<int, int> p1 = new Pair<int, int>(10, 12);

Pair<int, int> p2 = new Pair<int, int>(10, 11);

if (p1 == p2)

Console.WriteLine(“P1 is not equal to P2″);

if (p1 != p2)

Console.WriteLine(“P1 is not equal to P2″);

 

Dictionary<int, int> dictionary = new Dictionary<int, int>();

List<Pair<int, int>> list = new List<Pair<int, int>>();

list.Add(new Pair<int,int>(12, 10));

list.Add(new Pair<int,int>(11, 12));

list.Add(new Pair<int,int>(10, 13));

list.ForEach(l => Console.WriteLine(l));

Console.WriteLine();

 

dictionary = Pair<int, int>.ListToDictionary(list);

dictionary.Keys.ToList().ForEach(k =>

{

Console.WriteLine(k + “: “ + dictionary[k]);

});

Console.WriteLine();

 

list = Pair<int, int>.DictionaryToList(dictionary);

list.ForEach(l => Console.WriteLine(l));

Console.WriteLine();

 

list = new List<Pair<int, int>>();

list.Add(new Pair<int,int>(12, 10));

list.Add(new Pair<int,int>(12, 12));

list.Add(new Pair<int,int>(12, 13));

list.ForEach(l => Console.WriteLine(l));

Console.WriteLine();

 

var dictionary2 = Pair<int,int>.ListToDictionaryDimension(list);

dictionary2.Keys.ToList().ForEach(k =>

{

string tmp = string.Empty;

(dictionary2[k] as List<int>).ForEach(l => tmp += l.ToString() + ” “);

Console.WriteLine(k + “: “ + tmp);

});

Console.WriteLine();

Console.Read();

 

When someone has idea how to improve it let me know.

 

Happy Coding :)

About these ads

2 Responses to Generic Pair C#

  1. Stefan Gies December 22, 2012 at 1:09 pm

    Maybe you should override the Equals() method for Pair :

    public override bool Equals(object obj)
    {
    Pair pair = obj as Pair;
    if (Equals(pair, null)) return false;
    return ((Equals(First, null) && Equals(pair.First, null)) || (!Equals(First, null) && First.Equals(pair.First)))
    && ((Equals(Second, null) && Equals(pair.Second, null)) || (!Equals(Second, null) && Second.Equals(pair.Second)));
    }

    then you can change your operator methods:

    public static bool operator ==(Pair p, Pair q)
    {
    if (Equals(p, null) && Equals(q, null)) return true;
    if (Equals(p, null) || Equals(q, null)) return false;
    return p.Equals(q);
    }

    public static bool operator !=(Pair p, Pair q)
    {
    if (Equals(p, null) && Equals(q, null)) return false;
    if (Equals(p, null) || Equals(q, null)) return true;
    return !p.Equals(q);
    }

    This catches cases where T or U are strings or structs which may have overrides for Equals().

    • Stefan Gies December 22, 2012 at 1:12 pm

      Um, there seems to be a problem with encoding here…

      public override bool Equals(object obj)
      {
      Pair<T, U> pair = obj as Pair<T, U>;
      if (Equals(pair, null)) return false;
      return ((Equals(First, null) && Equals(pair.First, null)) || (!Equals(First, null) && First.Equals(pair.First)))
      && ((Equals(Second, null) && Equals(pair.Second, null)) || (!Equals(Second, null) && Second.Equals(pair.Second)));
      }

      public static bool operator ==(Pair<T, U> p, Pair<T, U> q)
      {
      if (Equals(p, null) && Equals(q, null)) return true;
      if (Equals(p, null) || Equals(q, null)) return false;
      return p.Equals(q);
      }

      public static bool operator !=(Pair<T, U> p, Pair<T, U> q)
      {
      if (Equals(p, null) && Equals(q, null)) return true;
      if (Equals(p, null) || Equals(q, null)) return false;
      return !p.Equals(q);
      }

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: