Skip to content

BRO1124: Refer to tuple elements by name

item.Count instead of item.Item1 when the element has a name. Replaces StyleCop SA1142.

Example

Before

public class Stock
{
    public (int Count, string Sku) Lookup() => (3, "A-1");

    public string Describe()
    {
        var item = Lookup();
        return item.Item2 + ": " + item.Item1;
    }
}

After

public class Stock
{
    public (int Count, string Sku) Lookup() => (3, "A-1");

    public string Describe()
    {
        var item = Lookup();
        return item.Sku + ": " + item.Count;
    }
}

Why

The name says what the element is; Item2 makes the reader look up the declaration.

How the fix works

ItemN is replaced with the element's name, also after ?. and for elements after the seventh (Item8).

Not reported

  • Elements without a name, System.Tuple (its Item1 is a property, not a tuple element), and nameof(t.Item1), whose result would change.
  • An element read through a receiver with the element's own name (x.Item1 where the element is x): x.x says nothing. Such names are usually inferred from another lambda's parameter (.Select(x => (x, F(x)))).

Compared with StyleCop

Same positions, except nameof(t.Item1) and x.Item1 with an element named x. Under dotnet format, StyleCop's fix throws (a null reference), so it changes nothing.