meonstackexchange
meonstackexchange

Reputation: 639

ref and out with value type variables

msdn documentation on out says that a parameter passed as out must be assigned a value inside the function. Example from the website:

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

According to my understanding when the int "value" is declared it is already assigned a default value of 0 (since int is a value type and cannot be null.) So why would it be necessary that "Method" modify its value?

Similarly, if "ref" were to be used instead of out, would there be any need of initializing "value"?

There are questions like this What's the difference between the 'ref' and 'out' keywords? but no one wants to put 2 and 2 together.

Upvotes: 2

Views: 1317

Answers (4)

Guffa
Guffa

Reputation: 700252

According to my understanding when the int "value" is declared it is already assigned a default value of 0 (since int is a value type and cannot be null.)

No, that's not correct.

A local variable isn't assigned a value when it is created. The space for local variables are created by moving the stack pointer to make room for them on the stack. That memory area is not cleared, it will contain any values that happened to be there.

The compiler forces you to assign a value to the variable before it can be used, so that the "garbage" value that it originally contains never will be used.

Similarly, if "ref" were to be used instead of out, would there be any need of initializing "value"?

The value wouldn't need to be set in the method, as it already has a value. The variable used to call the method would need to be initialised though:

static void Method(ref int i) {
    // doesn't need to set the value
}

static void Main() {
    int value;
    value = 42; // needs to be initialised before the call
    Method(ref value);
   // value is still 42
}

Upvotes: 5

Clock
Clock

Reputation: 984

Sometimes from your methods you need to return more than one value, so in that case you can return those extra values through ref and out parameteres. A good reference to these are the "TryParse" methods from .NET framework, for instance the "Int32.TryParse Method"

[1]: https://msdn.microsoft.com/en-us/library/f02979c7%28v=vs.110%29.aspx

You can find more on the above link in the MSDN.

Upvotes: 0

Tigran
Tigran

Reputation: 62246

You have to assign value to the variable. But the notion of out parameter, you've mentioned in post, has more semantical meaning.

If you pass out parameter you expect that the function you call has to assign it. C# compiler simply enforced that "good programmer" skill to compilation time control, for better code style.

Upvotes: 0

Rufus L
Rufus L

Reputation: 37020

An int is not assigned a value by default, even though there is a default value...try this:

Console.WriteLine(default(int)); // output = '0'
int foo;
Console.WriteLine(foo); // compile error - Use of unassigned local variable

The contract for out parameters is that the method is guaranteed to assign a value to it. The contract for ref parameters has no such guarantee.

Upvotes: 1

Related Questions