Why use the ref keyword to pass a string parameter in a function call

String is a reference type, so we need to attach the ref keyword in front of the string variable when we pass the function call to get the change in the main function For example:

using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { string keyword= string.Empty; removeWhiteSpacetest(keyword); Console.WriteLine("Printing In Main"); Console.ReadLine(); } } private void removeWhiteSpacetest( string keyword) { string pattern = "\\s+"; string replacement = " "; Regex rgx = new Regex(pattern); //white space removal keyword = rgx.Replace(keyword, replacement).Trim(); } } 

therefore, when I went through "hotel management", the output should be "hotel management", but I get the same result as "hotel management", and not the expected result "hotel management".

But when I use a list or some other object of reference type, I get the expected result, i.e. "hotel management"

0
c #
source share
2 answers

Update transmission by reference in accordance with the comments below.

When you add the ref keyword, you pass by value , passing a reference to the base value, and therefore you can change the value of the data. The control variable is like a pointer, not a value .

Read the MSDN article.

0
source share

Actually, this has nothing to do with an immutable parameter and is not related to the fact that the parameter is a reference type.

The reason you don't see the change is because you are assigning a new value to the parameter inside the function.

Parameters without the ref or out keywords are always passed by value.
I know that you are raising eyebrows now, but let me explain:
The parameter of the reference type, which is passed without the ref keyword, actually passes its reference by value.
For more information, read this Jon Skeet article .

To demonstrate my claim, I created a small program that you can copy and paste to see for yourself, or just check this script .

 using System; using System.Collections.Generic; public class Program { public static void Main() { String StringInput = "Input"; List<int> ListInput = new List<int>(); ListInput.Add(1); ListInput.Add(2); ListInput.Add(3); Console.WriteLine(StringInput); ChangeMyObject(StringInput); Console.WriteLine(StringInput); Console.WriteLine(ListInput.Count.ToString()); ChangeMyObject(ListInput); Console.WriteLine(ListInput.Count.ToString()); ChangeMyObject(ref StringInput); Console.WriteLine(StringInput); ChangeMyObject(ref ListInput); Console.WriteLine(ListInput.Count.ToString()); } static void ChangeMyObject(String input) { input = "Output"; } static void ChangeMyObject(ref String input) { input = "Output"; } static void ChangeMyObject(List<int> input) { input = new List<int>(); } static void ChangeMyObject(ref List<int> input) { input = new List<int>(); } } 

The output of this program is as follows:

 Input Input 3 3 Output 0 
0
source share

All Articles