Explanation why IEnumerable is more efficient than a List

前端 未结 7 1781
北荒
北荒 2021-01-30 08:35

I keep hearing that in .net 3.5 you should use IEnumerable over a List, but I can’t find any reference materials or articles that explain why it’s so much more proficient. Does

7条回答
  •  醉梦人生
    2021-01-30 09:11

    One reason that it is recommended to have methods return IEnumerable is that it is less specific than List. This means that you can later change the internals of your method to use something that may be more efficient for the needs of it, and as long as it is an IEnumerable you don't need to touch the contract of your method.

提交回复
热议问题