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.

2

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

7 Comments

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???
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.
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.
got it :) when we do abc = "Changed from Func1"; a new String object is created and refrence assign to variable abc.
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.
|
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.

2 Comments

"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???
No, even though you are able to modify the variable in the caller, you still need to have the correct type.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.