Why I need to use ref keyword in both declaration and Call?

a property or indexer may not be passed as an out or ref parameter
c# ref array
c# ref class
c# reference to object
c# pass object by reference
c# out
pass by reference c#
c# reference variable

Duplicate of: What is the purpose of the "out" keyword at the caller?

Why I need to use 'ref' keyword in both declaration and Call.

void foo(ref int i)
{

}

For example, Consider above function. If I will call it without ref keyword as

foo(k);

it will give me error :

Argument '1' must be passed with the 'ref' keyword

Why isn't it enough to just specify in the method signature only ?

This is because ref indicates that the parameter should be passed in by reference. It is something like pointer in C++

For example;

void CalFoo()
{
  var i=10;
  foo(ref i);  //i=11
}
void foo(ref int i)
{
   i++;
}

but

void CalFoo()
{
  var i=10;
  foo(i);  //i=10
}
void foo(int i)
{
   i++;
}

I think you can have both foo(ref int) and foo(int) in one single class. So if you don't specify the ref.. how does the compiler knows which one to call?

Why I need to use ref keyword in both declaration and Call?, Code examples of when to use C# ref and out parameters. Both indicate that an argument/parameter is passed by reference. in the method will be reflected in that variable when control returns to the calling method. by Ref is useful when the called method is also needed to modify the pass parameter. This time, the value of the parameter after the method execution is same to what it was set inside the method. The reason behind is that the use of ref keyword causes the parameter to be passed by reference and not by actual value. In other words, the actual parameter value was shared with the method due to use of ref keyword.

It enhances readability/understandability: When you look at the method call, you know that the ref value might change.

ref vs out in C#, This article helps you decide whether to use a ref or out keyword, especially when the We have two keywords named ref and out in C#. Then, we call this method and print the value of the parameter - before method call,� SetString1: This uses the ref keyword. Whenever we pass a string to this method, it must have the ref keyword. Then: When the method is called, the storage location of the string variable is copied to the method. SetString2: This has the out keyword. Whenever we want to call SetString2, we must use the out keyword on its argument.

C# ref Keyword, Whenever we want to call SetString2, we must use the out keyword on its Note: The ref argument in the NullIf method both reads and writes the string. It is an� The out and ref keyword look quite similar in nature. Both parameters are used to return back some value to the caller of the function. But still there is a small but important difference between them. Both of the parameter type has been kept in the C# language for specific scenario.

As the Method and the Caller may be "far" away from each other (Different Assemblies, you may not even have the source of the one containing the method), having to specify out and ref explicitly helps making the intent clear. So it's something for the user so that they really know what they do, not a technical necessity.

C# ref Keyword, C++ function call by reference - The call by reference method of passing arguments So accordingly you need to declare the function parameters as reference exchanges the values of the two integer variables pointed to by its arguments. int &y) { int temp; temp = x; /* save the value at address x */ x = y; /* put y into x */ y� The passing of value through ref parameter is useful when the called method also need to change the value of passed parameter. The declaring of parameter through out parameter is useful when a method return multiple values. When ref keyword is used the data may pass in bi-directional. When out keyword is used the data only passed in unidirectional.

While ref could most of the time be inferred from the method signature, it's always obligatory due to the fact that it can change the behavior of the code after the method call completely. Consider:

string hello = "world";
MyMethod(ref hello);
Console.WriteLine(hello);

If the ref keyword hadn't been there you would always expect the code to print out "world", while in reality, it can print anything.

C++ function call by reference, You can declare the reference parameters using the ref keyword. swap(ref int x , ref int y) { int temp; temp = x; /* save the value of x */ x = y; /* put y into x of b : { 0}", b); /* calling a function to swap the values */ n.swap(ref a, ref b); Console. It shows that the values have changed inside the swap function and this change� Basically both ref and out for passing object/value between methods. The out keyword causes arguments to be passed by reference. This is like the ref keyword, except that ref requires that the variable be initialized before it is passed. out: Argument is not initialized and it must be initialized in the method

C# - Passing Parameters by Reference, To declare a struct as a ref struct; As local reference The ref keyword must be used in the method definition as well as when the method is� I have confused that when I use ref or out keyword in c# side, What is type declaration in c++ side, and is there difference between the value types(int, struct) and class type? The c# code struct

ref in C#, But the main difference between ref and out keyword is that ref needs that But out parameter doesn't require the variables to be initialized For using out keyword as parameter both the method definition and calling method must use But from C# 7.0, excepting a separate variable declaration, the user� The base class that is accessed is the base class specified in the class declaration. For example, if you specify class ClassB : ClassA, the members of ClassA are accessed from ClassB, regardless of the base class of ClassA. Example. In this example, both the base class, Person, and the derived class, Employee, have a method named Getinfo.

Out Parameter With Examples in C#, Declaration of C# Pass By Reference. Following is a simple int x = 10; // Variable need to be initialized. Multiplication(ref x); reference (ref). To use the ref parameter in c# application, both the method definition and the calling method must explicitly use the ref keyword.

Comments
  • This is definitely a duplicate. Finding it now...
  • @Jon Skeet : I have tried to search but could not find it. I will close it if it is duplicate.
  • @Jon Skeet : Thanks... That thread has more information. Voted to close.
  • It wasn't me that added the reference - but that was the question I was thinking of :)
  • @Ngu Soon Hui : Ya I agree with that. But my question is why I need to use it in both Method call and declaration.
  • Ngu Soon Hui : Great thanks.. Now I understand why.. Will wait for few more answers... otherwise you rock.... +1
  • Because they int and ref int are different types, also for overloading. See the last sentence in this answer. If you would allow dropping the ref then your method call would have a different signature than the invoked method. Surely solvable, unless overloading strikes. But it could lead to all sorts of subtle bugs. Also you know explicitly that the method can change the object reference when calling methods with ref.
  • I wouldn't bring up the overloading issue at all for this question... The compiler for .NET does some very complex overloading resolving behind the scenes (especially now with the introduction of the dynamic type) and has errors for ambiguous cases. What's much more important with using the ref keyword is to explicitly show that the value might change in the method call.
  • You can have signatures int and ref int or int and out int, but you cannot have signatures ref int and out int.
  • @Lennaert : Sounds like a valid argument. Can you point me to some manual stating this, if you have?
  • Actually, I can't find anything about it, but the compiler is smart enough to infer that it should pass a ref type (I assume), that's how I came to the conclusion. Maybe I stated it a bit too confidently :)
  • This is surely the answer: it's obvious that C#'s designers did not have to require 'ref' at the call site (almost no other languages require it), so why else would it be required but to make code more clear? (okay, it's also for overloading as Jon Skeet points out, but that's very rarely done since other .NET languages can't overload that way)
  • You are right Jon.. It is duplicate of stackoverflow.com/questions/1393946/…. I have also voted to close it. Btw , You have also answered that question. Thanks.
  • @Jon : Btw, I had this question after reading your this ( yoda.arachsys.com/csharp/parameters.html ) article.
  • @Mahin: Good feedback - if I remember, I'll try to add a section to that article.