what is the "REF" and "OUT" method in C#
Loading
what is the "REF" and "OUT" method in C#
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Amit MohantyRank #112 · 20K reputationPosted Mar 5, 2023, 11:05 PM EST
The "REF" keyword is used to pass a reference to a variable as an argument to a method. When a variable is passed by reference, any changes made to the variable inside the method will also be reflected outside the method.
For example:
public void Increment(ref int x) { x++; } int y = 10; Increment(ref y); Console.WriteLine(y); // Output: 11The "OUT" keyword works similarly to "REF" but with one significant difference. The "OUT" keyword is used when a method returns multiple values or when a method does not return any value at all but instead uses its parameters to return values.
For example:
public void Divide(int dividend, int divisor, out int quotient, out int remainder) { quotient = dividend / divisor; remainder = dividend % divisor; } int x = 10, y = 3, q, r; Divide(x, y, out q, out r); Console.WriteLine("{0} / {1} = {2}, remainder {3}", x, y, q, r); // Output: 10 / 3 = 3, remainder 1For mode details check the below links.
https://www.c-sharpcorner.com/UploadFile/ff2f08/ref-vs-out-keywords-in-C-Sharp/
https://www.geeksforgeeks.org/difference-between-ref-and-out-keywords-in-c-sharp/
https://www.pluralsight.com/guides/csharp-in-out-ref-parameters
Naimish MakwanaRank #324 · 7.7K reputationPosted Mar 5, 2023, 11:09 PM EST
Hello Brijesh,
"ref" and "out" are two keywords in C# that are used to pass arguments to a method by reference instead of by value.
When an argument is passed by value, a copy of the argument is made and passed to the method. Any changes made to the argument within the method do not affect the original value of the argument outside the method.
However, when an argument is passed by reference using the "ref" keyword, the memory address of the argument is passed instead of a copy of its value. Any changes made to the argument within the method affect the original value of the argument outside the method.
The "out" keyword is similar to "ref", but it is used when a method needs to return multiple values. When a variable is passed using "out", the method can change the value of the variable, and that new value will be available to the caller of the method.
Example :
// using ref keyword void MultiplyByTwo(ref int x) { x = x * 2; } int number = 5; //You must have to assign value when use ref keyword MultiplyByTwo(ref number); Console.WriteLine(number); // Output: 10 // using out keyword void GetSumAndDifference(int x, int y, out int sum, out int difference) { sum = x + y; //You must assign value inside the method when use out keyword difference = x - y; } int a = 10; int b = 5; int total; int difference; GetSumAndDifference(a, b, out total, out difference); Console.WriteLine(total); // Output: 15 Console.WriteLine(difference); // Output: 5You can also refer below article:
https://www.c-sharpcorner.com/UploadFile/ff2f08/ref-vs-out-keywords-in-C-Sharp/
Thanks
Naimish Makwana
Arvind YadavRank #880 · 2.5K reputationPosted Mar 5, 2023, 11:10 PM EST
In C#, "REF" and "OUT" are keywords used to pass arguments to a method by reference instead of by value.
The "REF" keyword is used to pass arguments to a method by reference. When you pass an argument by reference, you pass the memory address of the variable rather than a copy of the variable's value. This means that any changes made to the parameter inside the method will also be reflected in the original variable passed in.
void GetValues(out int x, out int y) { x = 5; y = 10; } int myVar1, myVar2; GetValues(out myVar1, out myVar2); // myVar1 is now 5, and myVar2 is now 10In the example above, we pass
myVarby reference using the "REF" keyword. Inside theSomeMethodmethod, we multiply the value ofxby 2, which changes the value ofmyVaroutside the method as well.The "OUT" keyword is similar to "REF", but it is used when the method needs to return multiple values. When you pass an argument as "OUT", you are indicating that the method will assign a value to that parameter, but it doesn't expect the value to be assigned beforehand. In other words, the parameter is considered uninitialized when it is passed in.
void SomeMethod(ref int x) { x = x * 2; } int myVar = 5; SomeMethod(ref myVar); // myVar is now 10In the example above, we use the "OUT" keyword to return two values from the
GetValuesmethod. We pass inmyVar1andmyVar2as uninitialized variables, and inside the method, we assign values to both variables. After the method call, both variables now hold the values assigned inside the method.Jignesh KumarRank #91 · 22.9K reputationPosted Mar 5, 2023, 11:10 PM EST
Hello Brijesh,
Please refer this thread which will help you out to undestand with example,
https://www.c-sharpcorner.com/UploadFile/ff2f08/ref-vs-out-keywords-in-C-Sharp/
Vishal YelveRank #178 · 13.6K reputationPosted Mar 5, 2023, 11:54 PM EST
Hi Brijash,
Please refer below link
https://www.youtube.com/watch?v=lYdcY5zulXA
Tuhin PaulRank #7 · 71.9K reputationPosted Mar 6, 2023, 8:38 PM EST
Hello Brijesh,
In C#, "ref" and "out" are keywords used to pass arguments to a method by reference rather than by value. "Ref" allows a method to modify the value of an argument directly, while "out" is used to return a value from a method.
When using "ref", the argument must be initialized before it is passed to the method, and any changes made to the argument in the method are reflected in the calling code. On the other hand, "out" allows a method to assign a value to an argument, even if it was not initialized before being passed to the method.