Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# 'ref' keyword, performance

If you have a Bitmap object that needs to be passed to numerous methods (about 10), and finally to an event where it shall finally be disposed of after it's used, would it be (performance wise) beneficial to pass it to every one of those methods by reference instead of value?

Passing by value, the object is copied, passing by reference its not.

(Performance is critical in this situation. The application needs to run as fast as possible.)

like image 683
David Anderson Avatar asked May 23 '09 05:05

David Anderson


2 Answers

Bitmap is a reference type. Passing a reference type by value does not copy the object, merely the reference to the object. There would be no performance benefit to passing the Bitmap by reference instead of by value.

like image 64
David Nelson Avatar answered Oct 10 '22 19:10

David Nelson


Since Bitmap is a reference type, there is no practical difference for performance in this scenario as it is already being passed by reference to the method.

I'd recommend Jon Skeet's article on the subject for a thorough explanation of how "by reference" and "by value" work in C#.

like image 41
Jeromy Irvine Avatar answered Oct 10 '22 18:10

Jeromy Irvine