2

I have following code snippet. when i pass string strMain into foo method and print it then i get "main" string, if string is reference type then why it prints "main" not "local". when i do same thing by passing it with ref keyword then it works. string is reference types then why it needs ref keyword here.

void main()
{
   string strMain="main"
   foo(string str);
   Console.WriteLine(strMain);
}
void foo(string strFoo )
{
   strFoo="Local";
}
  • The "handle" (the reference) is a value and is passed as such (as a copy). In `foo` you simply change what the *copy* refers to. If you want to work on the same "handle" (reference) you need to declare the parameter `ref`. That would pass the reference by reference ;-). – Peter - Reinstate Monica Apr 14 '15 at 12:03
  • 1
    possible duplicate of [C# string reference type?](http://stackoverflow.com/questions/1096449/c-sharp-string-reference-type) – Jf Beaulac Apr 14 '15 at 12:21

1 Answers1

1

The reason this works the way it does is that you are passing strFoo by value, not by reference. Re-assigning strFoo inside foo to a new string has no effect on strMain from main. This is not specific to string: all reference types in C# work in this way.

If you want to make the assignment in foo be visible in main, pass your string by reference:

public void Main(string[] args) {
    string strMain="main";
    Foo(ref strMain);
    Console.WriteLine(strMain);
}
void Foo(ref string strFoo) {
     strFoo="Local";
}

Note that passing a reference type by value allows the method being called to change the value of the variable that you pass, as long as that variable is mutable. This cannot be done with string because it is immutable, but you can do it with a StringBuilder:

public void Main(string[] args) {
    StringBuilder strMain=new StringBuilder();
    Foo(strMain);
    Console.WriteLine(strMain);
}
void Foo(StringBuilder strFoo) {
     strFoo.Append("Local");
}

Here is an illustration:

Pass by value vs. pass by reference.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • in my given code strMain itself is reference type then why do we need to pass it with ref explicitly?? – Praveen Deewan Apr 14 '15 at 12:56
  • @PraveenDeewan Being a reference type has nothing to do with passing parameters by reference (I know, this sounds extremely confusing). When you pass by reference, the reference is to the variable (i.e. to `strMain`) is passed, and `strMain` is also a reference to the string `"main"`; essentially, you pass a double reference. When you pass by value, it's a single reference that's being passed - i.e. the reference to string `"main"`. – Sergey Kalinichenko Apr 14 '15 at 13:00
  • can you just explain me object creation scenario of this by drawing or any source link because i am not satisfied with this ref concept of reference variable. How it gets resolved. – Praveen Deewan Apr 14 '15 at 14:02
  • ahh... got the concept how it works with ref now totally satisfied, thanks a lot @dasblinkenlight – Praveen Deewan Apr 14 '15 at 14:31