4

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;
        }
    }

}
Penguen
  • 16,836
  • 42
  • 130
  • 205

4 Answers4

13

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

Craig Suchanec
  • 10,474
  • 3
  • 31
  • 39
  • 3
    Also, an out param has to be initialized by the function which requires it, otherwise you get compile errors, which is a good thing. –  May 29 '10 at 19:53
  • The caller does need to declare the variable, but does not need to initialize it. – TrueWill May 29 '10 at 21:49
6

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

shahkalpesh
  • 33,172
  • 3
  • 63
  • 88
3

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.

tster
  • 17,883
  • 5
  • 53
  • 72
1

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.

Lasse V. Karlsen
  • 380,855
  • 102
  • 628
  • 825