C# 7 – Returning multiple values using tuples

C# 7 Tuples

C# 7 release is coming soon and there is a great new feature I’d like to talk to you about – returning multiple values from a method using tuples.

Until now, we could either use out parameters (which are not “comfortable” ) or return a tuple in the following way

public Tuple<int,int,int> GetTime()
{
    int hour = 3;
        int minute = 4;
        int second = 5;
    return new Tuple<int,int,int>(hour,minute,second); 
}

This way of doing things have a few problems:

  1. Accessing the values is using Item1, Item2, and .Item3 properties of the tuple object, there are no names to tell which parameter is which (maybe we got the seconds and the minutes mixed up when reading the different items)
  2. The syntax of creating the tuple is very verbose and lengthy, its not comfortable to use
  3. The signature of the method is completely unreadable – what exactly is “Time” and how does it being described by three integers – we have to look at the code to do that (and if we need to read the code to understand what it does then we failed with naming)

Using out parameters we can relieve some of this downsides, but the syntax would remain verbose and not clean.

C# 7 to the rescue

Luckily C# 7 has a neat way of doing the same thing while keeping our code clean and readable. Take a look at the C# 7 version of the above

public (int hour, int minute, int second) GetTime()
{
        int hour = 3;
        int minute = 4;
        int second = 5;
        return (hour,minute,second); 
}

So whats happening here is that we can name each parameter, return all parameters using a clean syntax of joining all return values inside parenthesis with separating commas. The reader of this method would look as

var time = GetTime();
int hour = time.hour; \\etc'

which both takes the guesswork of understanding what each parameter mean and allows intellisense to help with autocomplete.

When NOT to use this feature

With such an exiting feature we can ask ourselves why not use it all the time. These are my thoughts:

  1. Consider creating a class containing the relevant data if its usage occurs multiple times or if it has specific domain meaning.
  2. Consider creating a class  if there is a behavior that can be encapsulated into a different type
  3. Sometimes out variables are more comfortable, as with the TryGetValue method, since we can condition on it “if(TryGetValue(..))”.  I wouldn’t like the bool value to be returned via a tuple here. C# 7 also helps makes out parameters more comfortable to use so don’t rule out using out parameters all together.

Summary

Using tuples in C# 7 is one of the features I am most exited about, it can make a huge difference writing clean readable code. That said, it’s not something you should use everywhere in code, and there are other constructs that are better suited in some cases, so have that in mind while deciding what to use.

Have fun and see you next time,

Amir

Advertisements

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 )

Google+ photo

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

Connecting to %s

Create a website or blog at WordPress.com

Up ↑

%d bloggers like this: