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.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Tuhin PaulPosted Mar 7, 2023, 1:38 AM
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.
Vishal YelvePosted Mar 6, 2023, 4:54 AM
Hi Brijash,
Please refer below link
https://www.youtube.com/watch?v=lYdcY5zulXA
Jignesh KumarPosted Mar 6, 2023, 4:10 AM
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/
Arvind YadavPosted Mar 6, 2023, 4:10 AM
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.
In 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.
In 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.Naimish MakwanaPosted Mar 6, 2023, 4:09 AM
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 :
You can also refer below article:
https://www.c-sharpcorner.com/UploadFile/ff2f08/ref-vs-out-keywords-in-C-Sharp/
Thanks
Naimish Makwana
Amit MohantyPosted Mar 6, 2023, 4:05 AM
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:
The "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:
For 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