S_pot

210518 C#_Nullable 본문

C#

210518 C#_Nullable

S_pot 2021. 5. 18. 17:26
 static void Main(string[] args)
        {
            int? a;  // Nullable 
            int b;   // NonNullable
            Console.WriteLine("1---------------------");
            a = null;
            //b = null;  // null값은 nullable 변수만 대입가능
            b = 0;
            
            Console.WriteLine("[" + a + "]");
            Console.WriteLine("[" + b + "]");
            Console.WriteLine("2---------------------");
            Console.WriteLine(a.HasValue);  // HasValue는 값이 null이면 False 그렇지 않으면 True
            //Console.WriteLine(b.HasValue);  // HasValue는 Nullable만 사용가능
            //Console.WriteLine("[" + a.Value + "]");  // 에러
            a = 100;
            Console.WriteLine(a.HasValue);
            Console.WriteLine("[" + a + "]");
            Console.WriteLine("[" + a.Value + "]");

        }

'C#' 카테고리의 다른 글

210520 C#_Console.WriteLine("{0}, {1}", 100, 200);  (0) 2021.05.20
210520 C#_IndexOf, LastIndexOf  (0) 2021.05.20
210518 C#_상수  (0) 2021.05.18
210518 C#_namespace  (0) 2021.05.18
210518 C#_소수 구하기  (0) 2021.05.18