C# Does Lambda => generate garbage?

前端 未结 2 1723
醉梦人生
醉梦人生 2021-02-02 10:23

Does using a lambda expression generate garbage for the GC opposed to the normal foreach loop?

// Lambda version
Foos.ForEach(f=>f.Update(gameTime));

// Norm         


        
2条回答
  •  耶瑟儿~
    2021-02-02 10:46

    In this case I think that you are using a generic method (ForEach) which will generate a new type (assuming that Foo is a reference type, only one new type will be generated), and the lambda will be compiled as a regular anonymous method. Nothing about that suggests any sort of linear increase in memory usage.

    As far as the profiler is concerned, you are not measuring anything about memory or the GC. You are measuring time spent executing the method, and the lambda should not be significantly slower than the 'regular' way.

提交回复
热议问题