C#

65.C# - Nullable (널 기능 형식)

Godffs 2009. 8. 17. 19:57
반응형

 정수형 등의 변수는 반드시 초기화해서 사용해야 한다.
변수 선언시 ?를 붙이면 null 값으로 초기화할 수 있다.

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

namespace Nulilable
{
    class Program
    {
        static void Main(string[] args)
        {
            int i = 100;
            //double nd = null; // ? 붙이지 않으면 null값으로 처리안됨
           
            int? ni = null;
            double? nd = 12.34;

            if (ni.HasValue) {        //널값이면 false, 값이 있으면 참
                Console.WriteLine("{0}", ni.Value);
            }
            else { Console.WriteLine("널값입니다"); }
        }
    }
}

결과화면


65.Nulilable.zip
반응형