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
Specific
728x90
'C# > ์ด๊ฒ์ด C#์ด๋ค' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
15. ๋ฉ์๋๋ก ์ฝ๋ ๊ฐ์ถ๋ฆฌ๊ธฐ + ์ฌ๊ทํธ์ถ ํจ์์ง๊ธฐ(ํผ๋ณด๋์น ์์ ) (0) | 2022.02.09 |
---|---|
14. ์ ์ถ๋ ฅ, ๋ถ๊ธฐ๋ฌธ (0) | 2022.02.08 |
12. ๋นํธ ์ฐ์ฐ์ (0) | 2022.02.08 |
11. null ์กฐ๊ฑด๋ถ ์ฐ์ฐ์ (0) | 2022.02.08 |
10. ๋ฌธ์์ด ๋ค๋ฃจ๊ธฐ (0) | 2022.02.07 |