What is diff Between Ref And Out?

.net, c#, syntax

Solution

- 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).

Problem

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.

Original source

Related problems