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

Time:04-23

I am trying to return named tuple from function and getting 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)>'

CodePudding user response:

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;
}

CodePudding user response:

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)

  • Related