Penguen
Penguen

Reputation: 17298

Why must use "out" instead of ref?

i wrote some code blocks about ref -out declaration. i think that ref is most useful out. Ok. why i need to use out. i can use always ref everytime:

namespace out_ref
{
    class Program
    {
        static void Main(string[] args)
        {
            sinifA sinif = new sinifA();
            int test = 100;
            sinif.MethodA(out test);
            Console.WriteLine(test.ToString());

            sinif.MethodB(ref test);
            Console.WriteLine(test.ToString());
            Console.ReadKey();
        }
    }

    class sinifA
    {

        public void MethodA(out int a)
        {
            a = 200;
        }

        int _b;
        public void MethodB(ref int b)
        {
            _b = b;
            b = 2*b;
        }
    }

}

Upvotes: 4

Views: 477

Answers (4)

Lasse V. Karlsen
Lasse V. Karlsen

Reputation: 391596

ref and out is handled the same way internally, they're both passing a variable by reference.

The difference is in the initialization semantics, who is responsible for ensuring the variable has a valid start value, and it is enforced by the compiler.

With out parameters, it's the method you call. Whatever value is in the variable you pass by reference before you call the method is irrelevant. The compiler will complain if the method you call has an execution path that fails to set a new value for its parameter. This means that whatever value is in the variable before you call the method is guaranteed to be overwritten, even if it is with the same value.

With ref parameters, it's the method that calls. The value of the variable will be passed to the method being called, which can then inspect its parameter and optionally change it. The compiler will complain the method that calls does not initialize the variable with a value before making the call.

So basically, it's all down to what the compiler will complain about. Is it the calling code that must ensure there is a value in the variable or is it the called code.

Upvotes: 1

tster
tster

Reputation: 18257

An out parameter is guaranteed to be initialized during the function. This provides a more strict contract to your caller. It also allows you to write code like this:

int i;
MyFunc(out i);
.. < use i > ..

without getting a compiler error for an uninitialized variable.

Upvotes: 3

shahkalpesh
shahkalpesh

Reputation: 33474

When you have a parameter with out attached to it, you don't need to initialize it, before you pass it on to the method that accepts it.

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

From: http://msdn.microsoft.com/en-us/library/ee332485.aspx

Upvotes: 6

Craig Suchanec
Craig Suchanec

Reputation: 10834

Yes you can use ref every time but they have different purposes. ref is used for when a parameter is both an input and an output. out is used when the parameter is an output only. It can be used to pass an input but it makes it so the user of a function does not need to declare an instance before using the function because you are in effect saying that you will guarantee an instance is created. It is especially useful in the TryXXX pattern when you are getting a value from a collection

Upvotes: 13

Related Questions