10

Possible Duplicates:
Pass by value vs Pass by reference performance C#.net

Did anyone already test if passing parameters by reference is significantly faster than just copying them?

But the main focus of the question is: Are there any disadvantages using the ref keyword as opposite to not using it?

Community
  • 1
  • 1
Fabio Milheiro
  • 8,100
  • 17
  • 57
  • 96

2 Answers2

23

No, it doesn't improve speed significantly, or anything at all. On the contrary, by using the ref keyword you are adding another level of indirection that only can make the code slower.

Parameters are normally passed by value, which means that they are copied. For simple values like int, it simply means that a copy of the value is placed on the stack.

For reference types like a string it means that a copy of the reference is placed on the stack. So, it doesn't mean that the entire object is copied, it's just the reference to the object that is copied.

You should generally not use the ref or out keywords, unless there is a special reason to do so.

Guffa
  • 687,336
  • 108
  • 737
  • 1,005
  • 2
    A small nitpick: If you have a *large* struct then passing it by ref might improve performance very slightly. Having said that, if your struct is so large that passing it by val noticeably impacts performance then you should seriously think about refactoring the type itself, and *not* about passing it around by ref. – LukeH Aug 17 '10 at 12:08
  • 1
    I dont understand, if only reference is passed when using `string` argument, how is that original variable isn't changed when changing passed one? There must be a new copy of string in memory (or am I wrong?). And if I am processing (or parsing, but not changing) really big string, wouldn't it be faster to pass it by `ref`? Thank you for explanation :). – Buksy Jan 03 '14 at 14:06
  • @Buksy: A parameter of a reference type (like string) passed by value means that a copy of the reference is passed. If you change the parameter, it doesn't affect the original reference. Another factor here is that strings are immutable, so you can never change the original string. If you send a string parameter by reference, you can change the original reference so that it points to a different string object. As strings are immutable, they don't need to be copied to ensure that a method can't change them, which makes passing strings by value efficient. – Guffa Jan 03 '14 at 14:41
5

There are value types and reference types in C#

In the case of reference types, passing them without the ref keyword means passing references. I didn't test it, but I would expect that implementers of the compiler and the .NET framework made passing them as fast as possible. I can't imagine that passing references to those references is faster than passing the references in the first place. It doesn't make sense.

In the case of value types, it's another story. If a struct is big, copying it is costly for sure, and passing a reference should be faster. But value types are meant to be value types for a reason. If you have a value type and you are concerned about the efficiency of passing it to functions, most probably you made a mistake making it a value type.

76484
  • 8,498
  • 3
  • 19
  • 30
Maciej Hehl
  • 7,895
  • 1
  • 22
  • 23