I’m a C++ programmer trying to wrap my head around C# and found something I’m concerned about.
To declare an array, you have to use the New operator right?
But wont that get the garbage collector involved?
Hypothetically if I had a function that was creating an array just for use within that function and it was being called thousands of times per frame, wouldn’t all those calls to the New operator create a significant overhead for the garbage collector?
In C++ these would be arrays declared on the stack that would drop out of scope as soon as the function ends and the performance overhead would be minimal but in C# I’m worried how much performance the garbage collector might take up in this case.
So am I thinking about this the right way?
Is the garbage collector involved here and will it cause problems?
Is there a better “C# way” to do this?
basically, don’t worry about the garbage collector, it knows what it’s doing and it’s really good at it.
If you are doing image stuff, then yes, you may run into problems… you can step around this though, for instance you can use stackalloc http://msdn.microsoft.com/en-us/library/cx9s2sy4.aspx
but it means your code will have to be set to “unsafe”.