Why does foreach fail to find my GetEnumerator extension method?

前端 未结 8 1998
暗喜
暗喜 2021-02-18 21:19

I\'m trying to make some code more readable. For Example foreach(var row in table) {...} rather than foreach(DataRow row in table.Rows) {...}.

8条回答
  •  情深已故
    2021-02-18 21:47

    The object collection in a foreach must implement System.Collections.IEnumerable or System.Collections.Generic.IEnumerable.

    If you have a very strong desire to enable this then you could create a wrapper class which implements IEnumerable and has a pointer to you DataTable. Alternatively, you could inherit DataTable in a new class and implement IEnumerable.

    Code readability is most often personal preference. I personally find your changes to the foreach statement less readable (but I am sure there are many on SO who agree with you).

提交回复
热议问题