Why must use "out" instead of ref ?

Posted by Phsika on Stack Overflow See other posts from Stack Overflow or by Phsika
Published on 2010-05-29T19:21:30Z Indexed on 2010/05/29 19:32 UTC
Read the original article Hit count: 224

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

}

© Stack Overflow or respective owner

Related posts about c#

Related posts about .NET