Hi,Plz have luk @Below code:- plz explain me .....whether its call by Call by Reference Or Call by value
class Program
{
public static void Input(string C)
{
C= "ABCD";
Console.WriteLine(C);
}
static void Main(string[] args)
{
string B = "EFGH";
Program.Input(B);
Console.ReadKey();
}
}
Output : ABCD
1)Looking at the example it shows its call by value becoz , if we use call by refrence then we have to use ref keyword which is not in this case so its call by value according me.
2)But then, if it is call by value it should print "EFGH", but rather its printing "ABCD" of input method .
Why plz explain..
Thnx in Advance
Loading
VulpesPosted Oct 1, 2013, 8:20 AM
When this variable is passed by value to the Input method, the parameter C receives a copy of this reference.
Consequently, both B and C refer to the string "EFGH" when the method starts.
However, C is assigned a reference to the string "ABCD" within the body of the method and this value is then printed to the console.
This assignment to C doesn't change B which still refers to "EFGH". So, when the method returns and the value of B is printed to the console it's still "EFGH".
It's important to realize that B and C represent different memory locations and so are independent of each other once the method is called.
Aman JenPosted Oct 1, 2013, 7:14 AM
VulpesPosted Oct 1, 2013, 6:32 AM
The output is:
VulpesPosted Oct 1, 2013, 6:11 AM
However, this doesn't prevent you from changing the value parameter within the method and printing it out.
The difference is that the change won't be persisted to the calling code as it would with a call by reference.