Tuple literals with C#

gpeipman
1,380 views

Open Source Your Knowledge, Become a Contributor

Technology knowledge has to be shared and made accessible for free. Join the movement.

Create Content

Tuple literals with C#

C# 7 introduces tuple literals that make it easier to work with tuples. The problem of tuples have been members with awkward names like Item1, Item2 etc. Tuple literals solve this problem by allowing tuple members have informative names.

Let's start with example where store locator is used to find nearest store for user. There is DummyStoreLocator class that is service layer class and this is something we cannot change (suppose we don't have control over it and we have to use what we have). GetNearestStore() method on Hello class is application layer method and this is what we want to improve.

Click Run to see the demo
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
·using System;
using SharedClasses;
namespace OutVarExample
{
class Program
{
private static DummyStoreLocator _storeLocator = new DummyStoreLocator();
static void Main()
{
var dummyLat = 0.0;
var dummyLon = 0.0;
Store store = null;
var distance = GetNearestStore(dummyLat, dummyLon, out store);
Console.WriteLine("Store: " + store?.Name);
Console.WriteLine("Distance:" + distance);
}
public·static double GetNearestStore(double lat, double lon, out·Store store)
{
store = _storeLocator.FindNearest(lat, lon);
if(store == null)
{
return -1;
}
return _storeLocator.DistanceFrom(store, lat, lon);
}
}
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Moving to tuples

The code above uses "out" variable to give back store from GetNearestStore() method is application layer. We can argue that returning two independent values from method means new model class or "out" variable but there's also third way - tuples.

Click Run to see the demo
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
·using System;
using SharedClasses;
namespace TupleExample
{
class Program
{
private static DummyStoreLocator _storeLocator = new DummyStoreLocator();
static void Main()
{
var dummyLat = 0.0;
var dummyLon = 0.0;
var storeTuple = GetNearestStore(dummyLat, dummyLon);
Console.WriteLine("Store: " + storeTuple.Item2.Name);
Console.WriteLine("Distance:" + storeTuple.Item1);
}
public static Tuple<double, Store> GetNearestStore(double lat, double lon)
{
var store = _storeLocator.FindNearest(lat, lon);
var distance = _storeLocator.DistanceFrom(store, lat, lon);
return new Tuple<double, Store>(distance, store);
}
}
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Moving to tuple literals

We were able to eliminate "out" variable from GetNearestStore() but we introduced not informative member names like Item1 and Item2. If we don't have method body opened in code editor we cannot say almost anything about these Item1 and Item2 members. Let's use now tuple literals to make things nice again.

Click Run to see the demo
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
·using System;
using SharedClasses;
namespace TupleLiteralExample
{
class Program
{
private static DummyStoreLocator _storeLocator = new DummyStoreLocator();
static void Main()
{
var dummyLat = 0.0;
var dummyLon = 0.0;
var storeTuple = GetNearestStore(dummyLat, dummyLon);
Console.WriteLine("Store: " + storeTuple.Store.Name);
Console.WriteLine("Distance:" + storeTuple.Distance);
}
public static (double Distance, Store Store) GetNearestStore(double lat, double lon)
{
var store = _storeLocator.FindNearest(lat, lon);
var distance = _storeLocator.DistanceFrom(store, lat, lon);
return (distance, store);
}
}
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

With tuple literals our code is cleaner and it is easier to understand for other developers working with us.

References

Open Source Your Knowledge: become a Contributor and help others learn. Create New Content