Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is using the 'ref' keyword for string parameters in methods good for performance in C#? [duplicate]

As a programmer who don't have a good idea about the .NET pipeline, I was wondering if using ref strings as parameters are good for performance in C#?

Let's say I have a method like this:

public int FindSomething(string text)
{
    // Finds a char in the text and returns its index
}

When I use this method, the compiler creates a copy of the text for the method, right?

But if I use the ref keyword:

public int FindSomething(ref string text)
{
    // Finds a char in the text and returns its index
}

.. the compiler should only send the text's pointer address...

So is it good for performance using ref like this?

like image 764
Ibrahim Ozdemir Avatar asked Jul 12 '15 13:07

Ibrahim Ozdemir


People also ask

What is use of ref keyword in method?

The ref keyword indicates that a value is passed by reference. It is used in four different contexts: In a method signature and in a method call, to pass an argument to a method by reference. For more information, see Passing an argument by reference.

Is ref faster C#?

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.

What is the difference between the ref and out method parameters?

ref is used to state that the parameter passed may be modified by the method. in is used to state that the parameter passed cannot be modified by the method. out is used to state that the parameter passed must be modified by the method.

What are the two keywords that can be used when passing parameters by reference?

Ref and out keywords in C# are used to pass arguments within a method or function. Both indicate that an argument/parameter is passed by reference. By default parameters are passed to a method by value. By using these keywords (ref and out) we can pass a parameter by reference.


1 Answers

When I use this method, the compiler creates a copy of the text for the method, right?

No, it doesn't. string is a reference type, and the compiler will create a new stack variable which points to the same string represented at a given memory address. It won't copy the string.

When you use ref on a reference type, there won't be a copy of the pointer to the string created. It will simply pass the already created reference. This is useful only when you want to create an entirely new string:

void Main()
{
    string s = "hello";
    M(s);
    Console.WriteLine(s);
    M(ref s);
    Console.WriteLine(s);
}

public void M(string s)
{
    s = "this won't change the original string";
}

public void M(ref string s)
{
    s = "this will change the original string";
}

So is it good for performance using ref like this?

The performance gains won't be noticeable. What will happen is other developers getting confused as to why you used ref to pass the string.

like image 160
Yuval Itzchakov Avatar answered Sep 17 '22 15:09

Yuval Itzchakov