Multiple IEnumerable implementations paradox

前端 未结 2 1075
梦谈多话
梦谈多话 2021-01-18 23:56
  1. I have a generic class A<T>, that implements IEnumerable<T[]>.
  2. I want to have a convenience wrapper B that inherits from A<c

2条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-01-19 00:53

    foreach loop does not use IEnumerable or IEnumerable implementation. You can use foreach even if your class doesn't implement any of them. The only thing it needs is GetEnumerator method that returns IEnumerator implementation.

    Check this question: How is foreach implemented in C#?

    That's why your class works with foreach and doesn't with ToArray().

提交回复
热议问题