Ramesh Dutta
Ramesh Dutta

Reputation: 99

C# How to return named tuple List<(int row, string message)>

I am trying to return a named tuple from the function and getting an error. here is a sample code.

public List<(int row, string message)> IsAnyTagNull()
{
    List<Tuple<int, string>> rows = new List<Tuple<int, string>>();

    for (int row = rowindex; row < (range.RowCount + (rowindex - 1)); row++)
    {
        rows.Add(new Tuple<int, string>(row, "Cell Tag is null of row " + row));
    }
    return  rows
}

Above code return error

Cannot implicitly convert type 'System.Collections.Generic.List<System.Tuple<int, string>>' to 'System.Collections.Generic.List<(int row, string message)>'

Upvotes: 1

Views: 3281

Answers (2)

Eldar
Eldar

Reputation: 10790

You should define your list like this: var rows = new List<(int row, string message)>();

Type Tuple<int, string> is interpreted as (int Item1, string Item2)

Upvotes: 1

D-Shih
D-Shih

Reputation: 46239

Because List<Tuple<int, string>> is different from List<(int row, string message)>

You can try to create a collection which is List<(int row, string message)> type instead of List<Tuple<int, string>>

public List<(int row, string message)> IsAnyTagNull()
{
    List<(int row, string message)> rows = new List<(int row, string message)>();
    rows.Add((1, "Cell Tag is null of row "));

    return rows;
}

Upvotes: 2

Related Questions