Visual Studio 2008 C# Sample 을 보다가 Nullable 타입에 대한 예제가 있어 테스트 하며 정리해 보았다.
cf)  http://code.msdn.microsoft.com/Visual-Studio-2008-C-d295cdba 



■ 기본적인 사용법

    class Program

    {

        static void Main(string[] args)

        {

            DisplayValue(1);

            DisplayValue(null);

        }


        // num.Value throws an InvalidOperationException if num.HasValue is false

        static void DisplayValue(int? num)

        {

            if (num.HasValue == true)

                Console.WriteLine("num = " + num);

            else

                Console.WriteLine("num = null");


            try

            {

                Console.WriteLine("value = {0}", num.Value);

            }

            catch (InvalidOperationException e)

            {

                Console.WriteLine(e.Message);

            }

        }

    }

HasValue 프로퍼티의 사용 방법과 nullable 타입이 null 값을 갖고 있을때 Value 프로퍼티를 사용하면 예외가 발생한다.


■ Boxing

    class Program

    {

        static void Main(string[] args)

        {

            // null 값을 갖는 Nullable 타입의 박싱

            int? a;

            object oa;

            

            a = null;


            // oa값에 null 값이 할당됨. boxing 일어나지 않음.

            oa = a;


            Console.WriteLine("Testing 'a' and 'boxed a' for null...");


            // Nullable 타입 변수는 null 값과 비교할 수 있다.

            if (a == null)

                Console.WriteLine("\t a == null");


            // 박스된 nullable 변수는 null 값과 비교할 수 있다.

            // nullable 값을 박싱할 때, HasValue 값이 false 값을 갖는다면

            // 참조값을 null로 설정하기 때문이다.

            if (oa == null)

                Console.WriteLine("\t oa == null");




            // Nullable 타입의 언박싱

            Console.WriteLine("Unboxing a nullable type...");

            int? b = 10;

            object ob = b;


            // 박스된 nullable 타입을 언박스 시킬 수 있다.

            int? unBoxedB = (int?)ob;

            Console.WriteLine("\t b = {0}, unBoxedB = {1}", b, unBoxedB);


            // 박스된 nullable 타입의 값이 null 이라면 언박스 된 값도 null 이 된다.

            int? unBoxedA = (int?)oa;

            if (oa == null && unBoxedA == null)

                Console.WriteLine("\t a and unBoxedA are null");




            // 박싱된 nullable 타입을 nullable이 아닌 타입에 언박싱 시킬 수 없다.

            Console.WriteLine("Attempting to unbox into non-nullable type...");


            try

            {

                int unBoxedA2 = (int)oa;

            }

            catch (Exception e)

            {

                Console.WriteLine("\t {0}", e.Message);

            }

        }

    }



■ ?? 연산자의 사용

    class Program

    {

        static void Main(string[] args)

        {

            // ?? 연산자의 사용

            // 예제에서 x의 값이 null 이 아니면 x를 리턴, null 이면 -1을 리턴한다.

            int? x = null;

            int y = x ?? -1;

            Console.WriteLine("y == " + y);



            // Nullable Int 타입의 값을 Int 타입에 저장하는 방법

            // default 키워드는 해당 타입의 기본값을 리턴한다.

            int i = GetNullableInt() ?? default(int);

            Console.WriteLine("i == " + i);



            // ?? 연산자는 참조 타입에 대해서도 사용할 수 있다.

            string s = GetStringValue();

            Console.WriteLine("s = {0}", s ?? "null");


        }


        static int? GetNullableInt()

        {

            return null;

        }


        static string GetStringValue()

        {

            return null;

        }

    }



 Demo source




cf)
Nullable 형식 사용(C# 프로그래밍 가이드)


@


Posted by six605
,