Tuples in C#
One of the new C# 4.0 enhancements is a Tuple class that interests me as I'm currently trying to learn functional programming with F#.
If your like me your current production environment is likely to remain in 3.5 framework for some time. This does not mean that we cannot take advantage of some of these enhancements. A quick search of tuple in C# brought many examples including:
This class makes a good candidate for encapsulating the IDictionary.TryGetValue idiom.
Given the following:
TryGetValue returns bool and the value if true.
Since TryGetValue is the recommended method over ContainsKey to reduce the number of accesses, using Tuples seems a natural way to capture the results of this or any operation that returns multiple values.
Using Tuples and extension methods:
We can then test:
Not an out variable in site!
Now if I could just grok monads...
One of the new C# 4.0 enhancements is a Tuple
If your like me your current production environment is likely to remain in 3.5 framework for some time. This does not mean that we cannot take advantage of some of these enhancements. A quick search of tuple in C# brought many examples including:
1: public class Tuple<T1, T2> : IEqualityComparer<Tuple<T1, T2>>
2: {
3: /// <summary>
4: /// First item
5: /// </summary>
6: public T1 First { get; private set; }
7:
8: /// <summary>
9: /// Second item
10: /// </summary>
11: public T2 Second { get; private set; }
12:
13: /// <summary>
14: /// Constructor
15: /// </summary>
16: public Tuple(T1 first, T2 second)
17: {
18: First = first;
19: Second = second;
20: }
21:
22: /// <summary>
23: /// Creates the specified first.
24: /// </summary>
25: public static Tuple<T1, T2> Create(T1 first, T2 second)
26: {
27: return new Tuple<T1, T2>(first, second);
28: }
29: }
This class makes a good candidate for encapsulating the IDictionary.TryGetValue idiom.
Given the following:
1: var dictionary = new Dictionary<int, string>
2: { {1, "One"},
3: {2, "Two"},
4: {3, "Three"},
5: {4, "Four"},
6: {5, "Five"}
7: };
8:
9: string currentValue;
10: var results = dictionary.TryGetValue(0, out currentValue);
TryGetValue returns bool and the value if true.
Since TryGetValue is the recommended method over ContainsKey to reduce the number of accesses, using Tuples seems a natural way to capture the results of this or any operation that returns multiple values.
Using Tuples and extension methods:
1: public static Tuple<bool, T2> TryGetTuple<T1, T2>(this IDictionary<T1, T2> dictionary, T1 key)
2: {
3: T2 results;
4: return dictionary.TryGetValue(key, out results)
5: ? Tuple<bool, T2>.Create(true, results)
6: : Tuple<bool, T2>.Create(false, default(T2));
7: }
We can then test:
1: [Test]
2: public void TestTryGetTuple()
3: {
4: var nameValues = new Dictionary<int, string>
5: {
6: {1, "One"},
7: {2, "Two"},
8: {3, "Three"},
9: {4, "Four"},
10: {5, "Five"}
11: };
12:
13: Assert.IsNotNull(nameValues.TryGetTuple(3).Second);
14: Assert.IsTrue(nameValues.TryGetTuple(5).First);
15: Assert.IsFalse(nameValues.TryGetTuple(6).First);
16:
17: for (var i = 0; i <= nameValues.Count; ++i )
18: Console.WriteLine(nameValues.TryGetTuple(i).Second ?? "(null)");
19: }
Not an out variable in site!
Now if I could just grok monads...
Comments