Use of C# var for implicit typing of System.Data.Datarow

前端 未结 3 926
闹比i
闹比i 2021-02-12 18:54
foreach (var row in table.Rows)
{
     DoSomethingWith(row);
}

Assuming that I\'m working with a standard System.Data.DataTable (which has

3条回答
  •  眼角桃花
    2021-02-12 19:28

    That's because Rows is DataRowCollection, which in turn is IEnumerable and not IEnumerable, which means that type inferred will be object.

    When you explicitly state type in foreach, you instruct c# to add cast to each call, which is why it works.

提交回复
热议问题