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.
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
refparameter though, the original variable that you passed in can get modified.Try this:
The output you will get is:
In
Func1a copy offoois created, which refers to the same String. But as soon as you assign it another value, the parameterabcrefers to another String.foois not modified and still points to the same string.In
Func2you pass a reference tofoo, so when you assignabca new value (i.e. a reference to another string), you are really assigningfooa new value.