2014년 2월 20일 목요일

16. [C#,닷넷교육]매개변수(ref, out, 값, 참조전달, Pass By Value, Pass By Reference),C#/WPF/닷넷WPF/ASP.NET/ADO닷넷/닷넷교육/닷넷강좌학원/닷넷공부/닷넷책/닷넷객체지향교육 아래 예제는 Swap 함수로 값 자체가 전달 된다. using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication2 { class Program { static void Swap(int a, int b) { int tmp = a; a = b; b = tmp; } static void Main(string[] args) { int a = 10; int b = 20; Console.WriteLine("a={0}, b={1}", a, b); Swap(a, b); Console.WriteLine("a={0}, b={1}", a, b); } } } [결과] a=10, b=20 a=10, b=20 이와는 별도로 참조를 전달하기 위한 방법이 있는데 ref와 out이 있다. - out 키워드 참조에 의한 전달을 할 때 사용하는 키워드 참조로 값을 넘길 때 참조할 대상을 초기화할 필요는 없다. 빈 깡통을 넘겨서 값을 얻어낼 때 사용(오라클 같은 DBMS 프로시저의 OUT 매개변수) 하는데 초기화된 배열을 넘기는 것도 가능하다. - ref와 out 키워드 ref : 참조할 변수는 반드시 초기화되어 있어야 한다. out : 참조할 변수가 반드시 초기화할 필요는 없다. 둘 다 참조로 전달된다. - out 키워들 사용하는 경우 함수로부터 값을 얻어낼 때 주로 사용하기 때문에 특별히 초기화를 할 필요가 없는 것이다. using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication2 { class OnjOutTest { //초기화안된 배열을 받아서 초기화 //out 매개변수의 경우 초기화시킨 배열을 넘길 수도 있다. static void FillArray1(out int[] arr) { //배열 초기화 arr = new int[3] { 5919, 4790, 4791 }; } //초기화된 배열을 받아서 값 변경 static void FillArray2(ref int[] arr) { if (arr == null) { arr = new int[3]; } arr[0] = 1111; arr[1] = 2222; } static void Main() { int[] onjArray; FillArray1(out onjArray); //배열출력 Console.WriteLine("배열(out parameter) : "); for (int i = 0; i < onjArray.Length; i++) { Console.Write(onjArray[i] + " "); } Console.WriteLine("Press Any Key to Next"); Console.ReadKey(); FillArray2(ref onjArray); //배열출력 Console.WriteLine("배열(ref parameter) : "); for (int i = 0; i < onjArray.Length; i++) { Console.Write(onjArray[i] + " "); } Console.WriteLine("Press Any Key to Next"); Console.ReadKey(); FillArray2(ref onjArray); } } } [결과] 배열(out parameter) : 5919 4790 4791 Press Any Key to Next 배열(ref parameter) : 1111 2222 4791 Press Any Key to Next [출처] 오라클자바커뮤니티 - http://www.oraclejavanew.kr/bbs/board.php?bo_table=LecCsharp&wr_id=143 자바 오라클/빅데이터 아이폰/안드로이드 닷넷/WPF 표준웹/HTML5 채용/취업무료교육 초보자코스 [기업100%환급]SQL기초에서 Schema Object까지 총 5일 40시간 02-24 [기업100%환급]PL/SQL,ORACLE HINT,TUNING 총 5일 40시간 03-03 SQL초보에서실전전문가까지 총 18일 54시간 03-10 SQL초보에서실전전문가까지 총 8일 56시간 03-01 [주말저녁]SQL기초에서 Schema Object까지 총 10일 40시간 03-01

16. [C#,닷넷교육]매개변수(ref, out, 값, 참조전달, Pass By Value, Pass By Reference),C#/WPF/닷넷WPF/ASP.NET/ADO닷넷/닷넷교육/닷넷강좌학원/닷넷공부/닷넷책/닷넷객체지향교육


아래 예제는 Swap 함수로 값 자체가 전달 된다.

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

namespace ConsoleApplication2
{
    class Program
    {
        static void Swap(int a, int b)
        {
            int tmp = a;
            a = b;
            b = tmp;
        }
        static void Main(string[] args)
        {
            int a = 10;
            int b = 20;

            Console.WriteLine("a={0}, b={1}", a, b);

            Swap(a, b);

            Console.WriteLine("a={0}, b={1}", a, b);
        }
    }
}


[결과]
a=10, b=20
a=10, b=20

이와는 별도로 참조를 전달하기 위한 방법이 있는데 ref와 out이 있다.

- out 키워드
참조에 의한 전달을 할 때 사용하는 키워드
참조로 값을 넘길 때 참조할 대상을 초기화할 필요는 없다.
빈 깡통을 넘겨서 값을 얻어낼 때 사용(오라클 같은 DBMS 프로시저의 OUT 매개변수) 하는데 초기화된 배열을 넘기는 것도 가능하다.

- ref와 out 키워드
ref : 참조할 변수는 반드시 초기화되어 있어야 한다.
out : 참조할 변수가 반드시 초기화할 필요는 없다.
둘 다 참조로 전달된다.

- out 키워들 사용하는 경우
함수로부터 값을 얻어낼 때 주로 사용하기 때문에 특별히 초기화를 할 필요가 없는 것이다.



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

namespace ConsoleApplication2
{
    class OnjOutTest
    {
        //초기화안된 배열을 받아서 초기화
        //out 매개변수의 경우 초기화시킨 배열을 넘길 수도 있다. 
        static void FillArray1(out int[] arr)
        {
            //배열 초기화
            arr = new int[3] { 5919, 4790, 4791 };
        }
        
        //초기화된 배열을 받아서 값 변경
        static void FillArray2(ref int[] arr)
        {
            if (arr == null) 
            {
                arr = new int[3];
            }

            arr[0] = 1111;
            arr[1] = 2222;
        }
    

        static void Main()
        {
            int[] onjArray;

            FillArray1(out onjArray);

            //배열출력
            Console.WriteLine("배열(out parameter) : ");

            for (int i = 0; i < onjArray.Length; i++)
            {
                Console.Write(onjArray[i] + " ");
            }

            Console.WriteLine("Press Any Key to Next");
            Console.ReadKey();

            FillArray2(ref onjArray);

            //배열출력
            Console.WriteLine("배열(ref parameter) : ");

            for (int i = 0; i < onjArray.Length; i++)
            {
                Console.Write(onjArray[i] + " ");
            }

            Console.WriteLine("Press Any Key to Next");
            Console.ReadKey();

            FillArray2(ref onjArray);

        }

    }
}


[결과]

배열(out parameter) :
5919 4790 4791 Press Any Key to Next
배열(ref parameter) :
1111 2222 4791 Press Any Key to Next

댓글 없음:

댓글 쓰기