Possible Duplicate:
Whats the difference between the 'ref' and 'out' keywords?
What is the difference between ref
and out
? I am confused about when to use ref
and out
. So please explain how to use ref
and out
, and in which situations.
ref is used to state that the parameter passed may be modified by the method. in is used to state that the parameter passed cannot be modified by the method. out is used to state that the parameter passed must be modified by the method.
The out is a keyword in C# which is used for the passing the arguments to methods as a reference type. It is generally used when a method returns multiple values. The out parameter does not pass the property. The ref is a keyword in C# which is used for the passing the arguments by a reference.
Ref. is an abbreviation for reference. It is written in front of a code at the top of business letters and documents. The code refers to a file where all the letters and documents about the same matter are kept.
Reference parameters : This method copies the reference to the memory location of an argument into the formal parameter. This means that changes made to the parameter affect the argument. Output parameters : This method helps in returning more than one value.
You use Out when you pass an un-initialized parameter and the method will have to initialize and fill that parameter (you get a warning or even error otherwise).
bool IsUserValid(string username);
void IsUserValid(string username, out bool valid);
The declarations above are roughly the same. It's easier to return the value, so in this case you will use the return type. But if your method also needs to return the birth date of the user you can't return both parameters in the return, you have to use out parameters to return one of them (or void the method and return both as out).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With