C#/์ด๊ฒ์ด C#์ด๋ค
13. null ๋ณํฉ ์ฐ์ฐ์
Rainbow๐Coder
2022. 2. 8. 12:43
728x90
a??b
a๊ฐ null์ด๋ฉด b ๋ฐํ
null์ด ์๋๋ฉด a ๊ทธ๋๋ก ๋ฐํ
null ๋ณํฉ ์ฐ์ฐ์ ??๋ null ์กฐ๊ฑด๋ถ ์ฐ์ฐ์์ฒ๋ผ ํ๋ก๊ทธ๋จ์์ ์ข ์ข ํ์ํ ๋ณ์/๊ฐ์ฒด์ null ๊ฒ์ฌ๋ฅผ ๊ฐ๊ฒฐํ๊ฒ ๋ง๋ค์ด์ค๋ค.
?? ์ฐ์ฐ์๋ ๋ ๊ฐ์ ํผ์ฐ์ฐ์๋ฅผ ๋ฐ์๋ค์ด๊ณ ์ผ์ชฝ ํผ์ฐ์ฐ์๊ฐ null์ธ์ง ํ๊ฐํ๋ค.
ํ๊ฐ ๊ฒฐ๊ณผ๊ฐ null์ด ์๋ ๊ฒ์ผ๋ก ๋ํ๋๋ฉด ํผ์ฐ์ฐ์๋ฅผ ๊ทธ๋๋ก ๋ฐํํ๊ณ ,
null์ธ ๊ฒ์ผ๋ก ํ๊ฐ๋๋ฉด ์ค๋ฅธ์ชฝ ํผ์ฐ์ฐ์๋ฅผ ๋ฐํํ๋ค.
<์์ค์ฝ๋>
using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static System.Console;
namespace Hello
{
class MainApp
{
static void Main(string[] args)
{
int? num = null;
WriteLine($"{num??0}");
num = 99;
WriteLine($"{num??0}");
string str = null;
string temp = "Default";
WriteLine($"{str??"Default"}");
str = "Specific";
WriteLine($"{str?? temp}");
}
}
}
<์ถ๋ ฅ>
0
99
Default
Specific728x90