상세 컨텐츠

본문 제목

[C#] null 조건부/ 병합 연산자

유니티

by simstealer 2022. 9. 13. 09:28

본문

null 조건부

using static System.Console;
using System.Collections;

namespace Practice01
{
    class Class1
    {
        static void Main()
        {
            ArrayList arrayList = null;
            WriteLine(arrayList); // null
            arrayList = new ArrayList();

            arrayList.Add(1);
            arrayList.Add(2);
            // null인지 판별
            WriteLine($"array의 개수는? : {arrayList?.Count}");
        }
    }
}

 

null 병합

int a = null;
// a는 null이기 때문에 0을 반환
Console.log($"{a ?? 0}");

a = 22;
// a는 null이 아니기 때문에 값(22)을 반환
Console.log($"{a ?? 0}");

'유니티' 카테고리의 다른 글

[C#] foreach  (0) 2022.09.13
[C#] Switch문 -> Switch식으로 변경  (0) 2022.09.13
[C#] 날짜 서식화  (0) 2022.08.11
[C#] 숫자 서식화  (0) 2022.08.11
[C#] 문자열 서식  (0) 2022.08.11

관련글 더보기

댓글 영역