Arrays and Garbage Collection in C#


I’ve recently started learning about GC and how to minimize allocations, and have a question about arrays:
Regardless of it’s type, and Array is an allocation, right? That being the case, do arrays declared within a function generate garbage as if it was a normal field within the class? Or, in other words:

class ClassWithArrays { int[] fieldArray = new int[3]; void Foo() { // Does this count as an allocation, same as the "fieldArray", even though it only lives // For the duration of the method? var localArray = new int[3]; } } 

submitted by /u/Gizambica
[link] [comments]

Leave a Reply