Click here to Skip to main content
15,890,717 members
Please Sign up or sign in to vote.
4.33/5 (2 votes)
See more:
Hi all,

I have a method which is
C#
int x = 0; 
string a = string.Empty; 
string b = string.Empty;
MyMethod(x, a, b);


Now this method will modify the values of a and b. If this is possible then why do we need ref keyword? Or this feature is only in latest C# 4.0?

Thanks,
Ankita
Posted
Updated 31-Jan-14 21:19pm
v2

Reference parameters don't pass the values of the variables used in the function member invocation - they use the variables themselves. Rather than creating a new storage location for the variable in the function member declaration, the same storage location is used, so the value of the variable in the function member and the value of the reference parameter will always be the same. Reference parameters need the ref modifier as part of both the declaration and the invocation .
 
Share this answer
 
Comments
ankum16 1-Feb-14 5:42am    
Still I am not clear with explanation. Here lets assume I have put ref keywords while passing variables to the method. Here variables are already initialized.

MyMethod(x, ref a, ref b); And it will use the same memory location only as its reference type. Now what is difference between MyMethod(x, ref a, ref b) and MyMethod(x, a, b), if both behaves in same way.
V5709 1-Feb-14 6:01am    
The example that makes the difference most obvious is using string. It's a reference type but you can't change it's value in the same way you can other reference types, you have to create a new instance of the type and change the reference to point to the new instance. If you don't specify ref you can't change where the reference points to. For more refer fallowing code.. hope it will clear your point.!

-----------------------------------
class Program
{
static void Main(string[] args)
{
string changeMe = "ImTheSame";
ChangeString(changeMe);
Console.WriteLine(changeMe); //Prints: Same string as old value

ChangeString(ref changeMe);
Console.WriteLine(changeMe); //Prints: Different string
}

static void ChangeString(string changeMe)
{
changeMe = "ImDifferentNow";
}

static void ChangeString(ref string changeMe)
{
changeMe = "ImDifferentNow";
}
}
------------------------------------------
Primitive data types are passed by value.
Thus if you change the value of these variables inside the method, the caller values will not be affected.

To ensure the values of these variables change across the caller and the called, you can use byRef.
This feature is available in all versions.
 
Share this answer
 
v2
Comments
BillWoodruff 1-Feb-14 4:51am    
'byRef is VB, and the code here is in C#.
Abhinav S 1-Feb-14 7:46am    
Oops. Ref for C#.
First of all, if you want variables 'a and 'b to be modified in the method, you don't have to pass them ! The two variables are declared outside the scope of the method, therefore any change made to them in the method will persist "outside" the method.

Passing parameters by reference is an important part of the programmer's toolbox, and you should fully understand it. In C# you need to practice using both 'ref and 'out modifiers to parameters, and understand the difference.

Both 'ref and 'out enable passing values or objects by reference: 'out does not require the passed reference to be initialized, and 'ref requires it be initialized. Assigning a value to the variable to passed using 'out before it is passed will result in a compile time error.

I suggest you study these articles by Jon Skeet very carefully: [^], [^]. If they are confusing ... too advanced for you right now ... then try Charles Petzold's book "Dot Net Zero" (free): [^].
 
Share this answer
 
Comments
ankum16 1-Feb-14 5:41am    
Still I am not clear with explanation. Here lets assume I have put ref keywords while passing variables to the method. Here variables are already initialized.

MyMethod(x, ref a, ref b); And it will use the same memory location only as its reference type. Now what is difference between MyMethod(x, ref a, ref b) and MyMethod(x, a, b), if both behaves in same way.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900