Enumerator Implementation: Use struct or class?

前端 未结 8 2645
再見小時候
再見小時候 2021-02-19 17:44

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

8条回答
  •  心在旅途
    2021-02-19 17:58

    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.

提交回复
热议问题