Enumerator Implementation: Use struct or class?

前端 未结 4 842
我在风中等你
我在风中等你 2021-02-19 17:46

I noticed that List defines its enumerator as a struct, while ArrayList defines its enumerator as a class. What\'s t

4条回答
  •  傲寒
    傲寒 (楼主)
    2021-02-19 18:18

    Reason List uses a struct enumerator is to prevent garbage generation in foreach statements. This is pretty good reason especially if you are programming for Compact Framework, because CF doesn't have generational GC and CF is usually used on low performance hardware where it can quickly lead to performance issues.

    Also, I don't think mutable structs are source of problems in examples some posted, but programmers that don't have good understanding of how value types work.

提交回复
热议问题