1

I just noticed .net allows us to do like this.

  public void Func1(ref String abc)
  {
  }

I was wondering "ref" keyword makes any sense???? as String is a Class(reference type)

Is it any different from.

 public void Func1(String abc)
 {
 }

I am just asking as i am confused. either missing some concept or they are one and the same thing and "ref" keyword has no sense in this context.

Filburt
  • 17,626
  • 12
  • 64
  • 115
Pritesh
  • 1,938
  • 7
  • 32
  • 46
  • Same as ["ref" keyword and reference types](http://stackoverflow.com/questions/4656184/ref-keyword-and-reference-types) – Matthew Flaschen Apr 23 '12 at 07:58
  • You should read the msdn documentation for the `ref` keyword, it pretty much explains it: http://msdn.microsoft.com/en-us/library/14akc2c7.aspx – Magnus Apr 23 '12 at 08:02

2 Answers2

2

Parameters are passed by value by default. If you pass a parameter into a method, the original variable does not get modified. If you pass a parameter as a ref parameter though, the original variable that you passed in can get modified.

Try this:

public void Func1(String abc) {
    abc = "Changed from Func1";
}

public void Func2(ref String abc) {
    abc = "Changed from Func2";
}

public void main() {
    string foo = "not changed";
    Func1(foo);
    Console.WriteLine(foo);
    Func2(ref foo);
    Console.WriteLine(foo);
}

The output you will get is:

not changed
Changed from Func2

In Func1 a copy of foo is created, which refers to the same String. But as soon as you assign it another value, the parameter abc refers to another String. foo is not modified and still points to the same string.
In Func2 you pass a reference to foo, so when you assign abc a new value (i.e. a reference to another string), you are really assigning foo a new value.

Botz3000
  • 39,020
  • 8
  • 103
  • 127
  • I tried it works as you mentioned. I am a bit confuse. String ---> Class, Class ---> Refrence type, Refrence type's value changes inside function. Whu change made by Func1 did not got reflected??? – Pritesh Apr 23 '12 at 08:21
  • Think of `foo` as just containing a pointer to the String object, not the object itself. If you copy `foo` (as in `Func1`), you assign a pointer to a new String object to a copy of `foo`, so the original variable doesn't get changed and still points to the same String object. – Botz3000 Apr 23 '12 at 08:26
  • Suppose I have a class like public class Employee{ public float rate = 10; } Noe if I pass it to function Foo(Employee) and assign “rate” value 20, afterward when I access I will get rate 20. This my understanding of pass by reference. Changes made inside function are reflected outside. Why that did not happen in case of String. – Pritesh Apr 23 '12 at 08:33
  • 1
    You need to make a difference between the content of the variable (a pointer to an object) and the object itself. The variable contains only the pointer to the object. If the data of the object changes, the content of the variable itself does not change, because it still points to the same object. Just the data at the location it points to has changed. EDIT: If you assign the parameter of Foo(Employee) a new Employee(), it will not get reflected either. – Botz3000 Apr 23 '12 at 08:35
  • 1
    got it :) when we do abc = "Changed from Func1"; a new String object is created and refrence assign to variable abc. – Pritesh Apr 23 '12 at 09:18
  • finaly i figured out. = operator is overloaded so abc = "Changed from Func1"; results in abc = new string("Changed from Func1"); – Pritesh Apr 24 '12 at 09:10
  • 1
    Yes, that makes it even more clear that you are creating a new object. But even if it's an already existing string, like `abc = myOtherString`, abc points to another object after assigning. So it doesn't really matter if it's a new string, it just matters that it's a different string than the one that abc pointed to before. – Botz3000 Apr 24 '12 at 09:16
1

By default without the ref keyword, a copy of the string pointer is made (pass-by-value). Using ref will pass-by-reference and this also allows you to modify the pointer in the original caller.

David Z.
  • 5,621
  • 2
  • 20
  • 13
  • "allows you to modify the pointer in the original caller" does that mean i can make abc point to an objcet some other type (Class) like Employee??? – Pritesh Apr 23 '12 at 08:26
  • 1
    No, even though you are able to modify the variable in the caller, you still need to have the correct type. – David Z. Apr 23 '12 at 08:28