Ref |
Out |
The parameter or argument must be initialized first before it is passed to ref. |
It is not compulsory to initialize a parameter or argument before it is passed to an out. |
It is not required to assign or initialize the value of a parameter (which is passed by ref) before returning to the calling method. |
A called method is required to assign or initialize a value of a parameter (which is passed to an out) before returning to the calling method. |
Passing a parameter value by Ref is useful when the called method is also needed to modify the pass parameter. |
Declaring a parameter to an out method is useful when multiple values need to be returned from a function or method. |
It is not compulsory to initialize a parameter value before using it in a calling method. |
A parameter value must be initialized within the calling method before its use. |
When we use REF, data can be passed bi-directionally. |
When we use OUT data is passed only in a unidirectional way (from the called method to the caller method). |
Both ref and out are treated differently at run time and they are treated the same at compile time. |
|
Properties are not variables, therefore it cannot be passed as an out or ref parameter. |
Both ref and out are treated differently at run time and they are treated the same at compile time, so methods cannot be overloaded if one method takes an argument as ref and the other takes an argument as an out.
Example code
Output when the code is compiled:
However, method overloading can be possible when one method takes a ref or out argument and the other takes the same argument without ref or out.
Example Code
The out and ref keywords are useful when we want to return a value in the same variables as are passed as an argument.
1. https://www.c-sharpcorner.com/UploadFile/ff2f08/ref-vs-out-keywords-in-C-Sharp/
Скачано с www.znanio.ru
© ООО «Знанио»
С вами с 2009 года.