C# 'ref' keyword, performance

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.


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#.


Which type are you using exactly for holding the Bitmap? For example, System.Drawing.Bitmap is a reference type/class. When you pass a reference to a method (as an argument), the reference is passed by value. (A copy of the reference is made... not the object) So four bytes would be allocated on a 32-bit machine to hold the copy.

Using the ref keyword has not much bearing on performance except that the same reference is passed (a copy of the reference is not made). It has the following benefits

  • Only clears the intent that the method taking the parameter may modify it, and the caller may get a modified value post execution.
  • And the variable must be initialized by the callee before being passed as an argument to the called function taking the ref parameter.