Pair

I first used the Pair and Triplet classes while figuring out how to recreate dynamic ASP.NET WebForms controls. They came in pretty handy. Since then, generics came out and I created this(I'm sure I'm not alone):

/// 
/// Responsible for representing two values of any type.
/// 
/// The type of the first.
/// The type of the second.
public class Pair{
    /// 
    /// Gets or sets the first.
    /// 
    /// The first.
    public TFirst First { get; set; }

    /// 
    /// Gets or sets the second.
    /// 
    /// The second.
    public TSecond Second { get; set; }

    /// 
    /// Initializes a new instance of the  class.
    /// 
    public Pair() : this(default(TFirst), default(TSecond)) { }

    /// 
    /// Initializes a new instance of the  class.
    /// 
    /// The first.
    /// The second.
    public Pair(TFirst first, TSecond second){
    	this.First = first;
    	this.Second = second;
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33

Usage includes those times when you have the need for a structure to hold two simple values but you don't want to create a new class just for that. Say you want a dictionary where the key is an int and the value is that structure, you can do this with the Pair class:

var dict = new Dictionary();
dict.Add(1, new Pair("Carter", "Chris"));
dict.Add(2, new Pair("Carter", "Emmitt"));
foreach(var kvp in dict){
    Console.WriteLine(kvp.Value.First + ", " + kvp.Value.Second);
}
1
2
3
4
5
6

And the output would be this:

Kind of a lame example but you get the idea. I had made a similar one for the Triplet class but that started looking a little messy so I put that on the back burner.

 
Author: , 0000-00-00