프로그래밍/.NET

ref, out 키워드

건깡 2012. 8. 2. 10:44

ref 와 out 키워드

 - ref : 참조할 변수는 반드시 초기화되어 있어야 한다.

 - out : 참조할 변수가 반드시 초기화될 필요는 없다.


out  키워드를 사용하는 경우

 - 함수로부터 값을 얻어낼 때 주로 사용.


소스

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace TestReference
{
    class Program
    {
        static void CallByRef(ref int x)
        {
            x = 1000;
        }

        static void CallByOut(out int x)
        {
            x = 100;
        }

        static void Main(string[] args)
        {
            int RefX = 10;
            int OutX;
            CallByRef(ref RefX);
            CallByOut(out OutX);
            Console.WriteLine("Call-By-Ref: {0}", RefX);
            Console.WriteLine("Call-By-Ref: {0}", OutX);
        
        }
    }
}


출처 - 소셜같은 C#