DEV Community

Zohar Peled
Zohar Peled

Posted on • Originally published at zoharpeled.wordpress.com

4 1

Value Tuples makes actions better

(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
}
Enter fullscreen mode Exit fullscreen mode

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
}
Enter fullscreen mode Exit fullscreen mode

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.

Postmark Image

Speedy emails, satisfied customers

Are delayed transactional emails costing you user satisfaction? Postmark delivers your emails almost instantly, keeping your customers happy and connected.

Sign up

Top comments (1)

Collapse
 
saint4eva profile image
saint4eva

Value tuples added great value to C# programming language.

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more