loading...

Value Tuples makes actions better

peledzohar profile image Zohar Peled Originally published at zoharpeled.wordpress.com ・1 min read

(First published on What the # do I know?)

After a long wait, c#7 finally introduced value tuples - we no longer have to work with those awful Item1, Item2...Itemn properties that System.Tuple provides - we now have named tuples - meaning a much more readable code.

How does it help with actions?
Instead of using an Action<T1, T2, T3> With c#7 you can use an Action<(T1, T2, T3)>. Now I know that it doesn't seem like much when you look at it like this - but take a real life example and the difference should be painfully obvious:

// This is a part of a text file parser:

public void ReadFile(
    string fullPath, 
    Action<int, string, Exception> onReadError) 
{
    // implementation here
}

vs:

// This is a part of a text file parser:

public void ReadFile(
    string fullPath, 
    Action<(int RowNumber, string RowContent, Exception ReadException)> onReadError) 
{
    // implementation here
}

Using value tuples enables you to name your parameters - which makes your code far more readable and easy to understand and to use - since these names will also be visible in intellisense.

Posted on by:

peledzohar profile

Zohar Peled

@peledzohar

By day, try to work. By night, try to sleep.

Discussion

pic
Editor guide
 

Value tuples added great value to C# programming language.