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?
No, it doesn’t improve speed significantly, or anything at all. On the contrary, by using the
refkeyword 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
stringit 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
reforoutkeywords, unless there is a special reason to do so.