-
Notifications
You must be signed in to change notification settings - Fork 0
/
Tuple.cs
107 lines (99 loc) · 4.81 KB
/
Tuple.cs
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
// ----------------------------------------------------------------------------
// Tuple structs for use in .NET Not-Quite-3.5 (e.g. Unity3D).
//
// Used Chapter 3 in http://functional-programming.net/ as a starting point.
//
// Note: .NET 4.0 Tuples are immutable classes so they're *slightly* different.
// ----------------------------------------------------------------------------
using System;
namespace Binocle
{
/// <summary>
/// Utility class that simplifies cration of tuples by using
/// method calls instead of constructor calls
/// </summary>
public static class Tuple
{
/// <summary>
/// Creates a new tuple value with the specified elements. The method
/// can be used without specifying the generic parameters, because C#
/// compiler can usually infer the actual types.
/// </summary>
/// <param name="item1">First element of the tuple</param>
/// <param name="second">Second element of the tuple</param>
/// <returns>A newly created tuple</returns>
public static Tuple<T1, T2> Create<T1, T2>(T1 item1, T2 second)
{
return new Tuple<T1, T2>(item1, second);
}
/// <summary>
/// Creates a new tuple value with the specified elements. The method
/// can be used without specifying the generic parameters, because C#
/// compiler can usually infer the actual types.
/// </summary>
/// <param name="item1">First element of the tuple</param>
/// <param name="second">Second element of the tuple</param>
/// <param name="third">Third element of the tuple</param>
/// <returns>A newly created tuple</returns>
public static Tuple<T1, T2, T3> Create<T1, T2, T3>(T1 item1, T2 second, T3 third)
{
return new Tuple<T1, T2, T3>(item1, second, third);
}
/// <summary>
/// Creates a new tuple value with the specified elements. The method
/// can be used without specifying the generic parameters, because C#
/// compiler can usually infer the actual types.
/// </summary>
/// <param name="item1">First element of the tuple</param>
/// <param name="second">Second element of the tuple</param>
/// <param name="third">Third element of the tuple</param>
/// <param name="fourth">Fourth element of the tuple</param>
/// <returns>A newly created tuple</returns>
public static Tuple<T1, T2, T3, T4> Create<T1, T2, T3, T4>(T1 item1, T2 second, T3 third, T4 fourth)
{
return new Tuple<T1, T2, T3, T4>(item1, second, third, fourth);
}
/// <summary>
/// Extension method that provides a concise utility for unpacking
/// tuple components into specific out parameters.
/// </summary>
/// <param name="tuple">the tuple to unpack from</param>
/// <param name="ref1">the out parameter that will be assigned tuple.Item1</param>
/// <param name="ref2">the out parameter that will be assigned tuple.Item2</param>
public static void Unpack<T1, T2>(this Tuple<T1, T2> tuple, out T1 ref1, out T2 ref2)
{
ref1 = tuple.Item1;
ref2 = tuple.Item2;
}
/// <summary>
/// Extension method that provides a concise utility for unpacking
/// tuple components into specific out parameters.
/// </summary>
/// <param name="tuple">the tuple to unpack from</param>
/// <param name="ref1">the out parameter that will be assigned tuple.Item1</param>
/// <param name="ref2">the out parameter that will be assigned tuple.Item2</param>
/// <param name="ref3">the out parameter that will be assigned tuple.Item3</param>
public static void Unpack<T1, T2, T3>(this Tuple<T1, T2, T3> tuple, out T1 ref1, out T2 ref2, T3 ref3)
{
ref1 = tuple.Item1;
ref2 = tuple.Item2;
ref3 = tuple.Item3;
}
/// <summary>
/// Extension method that provides a concise utility for unpacking
/// tuple components into specific out parameters.
/// </summary>
/// <param name="tuple">the tuple to unpack from</param>
/// <param name="ref1">the out parameter that will be assigned tuple.Item1</param>
/// <param name="ref2">the out parameter that will be assigned tuple.Item2</param>
/// <param name="ref3">the out parameter that will be assigned tuple.Item3</param>
/// <param name="ref4">the out parameter that will be assigned tuple.Item4</param>
public static void Unpack<T1, T2, T3, T4>(this Tuple<T1, T2, T3, T4> tuple, out T1 ref1, out T2 ref2, T3 ref3, T4 ref4)
{
ref1 = tuple.Item1;
ref2 = tuple.Item2;
ref3 = tuple.Item3;
ref4 = tuple.Item4;
}
}
}