Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is diff Between Ref And Out? [duplicate]

Tags:

syntax

c#

.net

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.

like image 566
Cute Avatar asked Jun 19 '09 06:06

Cute


People also ask

What is the difference between out and ref?

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.

What is the difference between ref & out parameters with example?

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.

What does REF mean in code?

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.

How are output parameters different from reference parameters?

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.


1 Answers

  • You use Ref when you pass an initialized parameter and you expect the method/function to modify it.
  • 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).

like image 115
AlexDrenea Avatar answered Nov 14 '22 23:11

AlexDrenea