-3

In C#, what is the difference between ref and out parameter?

Example:

void method1(ref int p1)
{
 //....
}

void method2(out int p2)
{
 //....
}

Also, what are the guidelines for the use of each?

Scott.N
  • 172
  • 2
  • 17
  • 3
    Well, they both don't compile, so explaining code that is invalid isn't really meaningful. – Servy Apr 24 '13 at 16:09
  • 4
    Neither of these are valid declarations. You haven't specified a type for either parameter, and `in` is a keyword which isn't used in parameter declarations. After fixing that, please tell us what research you tried before asking the question. – Jon Skeet Apr 24 '13 at 16:09
  • 1
    Consider it pseudo-code, we can answer this question, it's obvious what he wants to know. – Jasmine Apr 24 '13 at 16:10
  • See [MSDN Reference for `out`](http://msdn.microsoft.com/en-US/library/t3c3bfhx(v=vs.80).aspx) – tnw Apr 24 '13 at 16:10
  • 2
    @Jasmine: Yes, but it's equally obvious that he hasn't put in even enough effort to give valid sample code, let alone try to research the issue properly. Basically this is an awful question. – Jon Skeet Apr 24 '13 at 16:11
  • @Jasmine it should also be obvious that this is something that's covered by every book/doc on C#. – Brian Rasmussen Apr 24 '13 at 16:11
  • And now it's been changed from `in` to `ref`, which is *completely* different... but still covered comprehensively in online documentation. (And even after the edit, it still wouldn't compile.) – Jon Skeet Apr 24 '13 at 16:12
  • Yes, but I've been told that SO is not the site where we tell people their question is bad, we're supposed to just answer it. – Jasmine Apr 24 '13 at 16:13
  • 3
    @Jasmine: I don't know where you were told that, but it's very clearly wrong. There are plenty of bad questions, and they're routinely downvoted, closed and deleted. – Jon Skeet Apr 24 '13 at 16:14
  • 1
    @Jasmine, I think the whole point of this site is the complete reverse of your statement. It's a **resource** of **good** questions. – Liam Apr 24 '13 at 16:17
  • possible duplicate of [What's the difference between the 'ref' and 'out' keywords?](http://stackoverflow.com/questions/388464/whats-the-difference-between-the-ref-and-out-keywords) – eandersson Apr 24 '13 at 16:18

2 Answers2

12

Both a ref parameter and an out parameter make an alias of a variable. That is, when you say:

M(int x) { ... }
...
int y = 123;
M(y);

then the value of y is passed, and x gets a copy of that value.

When you say

N(ref int x) { ... }
...
int y = 123;
N(ref y);

Then x and y are two names for the same variable. A change to x makes a change to y.

The only difference between ref and out from the caller's perspective is that the variable aliased by ref must be definitely assigned before the call. This is legal:

O(out int x) { ... }
...
int y; // no initialization!
O(out y);

But this is not:

N(ref int x) { ... }
...
int y; // no initialization!
N(ref y);

The only differences from the callee's perspective are:

  • a ref parameter can be assumed to be initially assigned; an out parameter cannot.
  • an out parameter must be assigned before control leaves the method normally. That is, this is illegal:

    void O(out int x) { return; }

    x must be assigned before the return.

Behind the scenes, ref and out have the same implementation; the only difference is how the compiler tracks whether variables are assigned or not.

The guidelines for their use are:

  • If you can avoid them, avoid them.
  • If you cannot avoid them, use "out" when you are logically returning a value via modifying the variable and "ref" when you are reading the value of the variable and then possibly modifying the variable. An out must modify, and must not read (before the modification). A ref may modify and can read before modification.
svick
  • 236,525
  • 50
  • 385
  • 514
Eric Lippert
  • 647,829
  • 179
  • 1,238
  • 2,067
  • I've always thought there ought to be a better term than "callee" but I've never come across one. If you ever find a good one which could be widely used in its stead, I'd love to hear about it. – Jon Skeet Apr 24 '13 at 16:31
4

Straight from MSDN documentation:

The out keyword causes arguments to be passed by reference. This is similar to the ref keyword, except that ref requires that the variable be initialized before being passed.

Source:http://msdn.microsoft.com/en-US/library/t3c3bfhx(v=vs.80).aspx

I feel that's clear enough. If this doesn't answer your question, let me know.

Another copy-paste of an example for the lazy:

class OutExample
{
    static void Method(out int i)
    {
        i = 44;
    }
    static void Main()
    {
        int value;
        Method(out value);
        // value is now 44
    }
}

So in this case, if it were Method(ref int i) instead, that method would require that i be initialized first.

tnw
  • 13,521
  • 15
  • 70
  • 111