본문 바로가기
C#

Nullable(널러블)

by Mostlove 2023. 7. 12.
728x90
반응형

using System;
using System.Collections;
using System.Reflection;

namespace CSharp1
{
    class Program
    {
        static int Find()
        {
            return 0;
        }
        class Monster
        {
            public int Id { get; set; }
        }

        static void Main(string[] args)
        {
            Monster monster = null;
            if (monster != null)
            {
                int monsterId = monster.Id;
            }

            int? id = monster?.Id;
            //위아래 같은 뜻
            if (monster == null)
            {
                id = null;
            }
            else
            {
                id = monster.Id;
            }

            //Nullable ->Null + able
            int? number = null;
            int b = number ?? 0;
            Console.WriteLine(b);
            if (number != null)
            {
                int a = number.Value;
                Console.WriteLine(a);
            }
            if (number.HasValue)
            {
                int a = number.Value;
                Console.WriteLine(a);
            }
           
        }
    }
}

반응형

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

Reflection(리플렌션)  (0) 2023.07.12
Exception(예외 처리)  (0) 2023.07.12
Lambda(람다식)  (0) 2023.07.12
Event(이벤트)  (0) 2023.07.12
Delegate(대리자)  (0) 2023.07.10