๐ป Programming Language/C#
29-1. Null ๋ณํฉ ์ฐ์ฐ์(Null Operator)
S.Honey
2022. 4. 7. 09:40
?
: Nullable ํ์ ์ ์ ์ธํ ๋ ์ฌ์ฉํ๋ ์ฐ์ฐ์??
๋(Null) ๋ณํฉ ์ฐ์ฐ์a ?? 100
์์ a ๊ฐ์ด null์ด๋ฉด 100์ ๋ฆฌํดํ๊ณ , a๊ฐ null์ด ์๋๋ฉด a์ ๋ณธ๋๊ฐ์ ๋ฆฌํดํ๋ค.๊ฐ์ด null์ด ๋๋ ๊ฒ์ ๋ง๊ธฐ์ํด ์ฌ์ฉํ๋ ๋ฏํ๋ค. (๊ฐ์ธ์ ์ธ ์๊ฒฌ)
namespace NullOperatorEx
{
class Program
{
static void Main(string[] args)
{
int? bb = null;
Console.WriteLine($"{bb ?? 10}");
bb = 12;
Console.WriteLine($"{bb ?? 10}");
string str1 = null;
// string ํ์
์ nullable์ด ๊ฐ๋ฅํ๊ธฐ์ ๋ฐ๋ก nullable์ฐ์ฐ์(`?`)๊ฐ ๋ถ์ง ์์๋ ๋๋ค.
Console.WriteLine($"{str1 ?? "null ๊ฐ์
๋๋ค."}");
string str2 = "null์ด ์๋๋๋ค.";
Console.WriteLine($"{str2 ?? "null ๊ฐ์
๋๋ค."}");
}
}
}
Output
10
12
null ๊ฐ์
๋๋ค.
null์ด ์๋๋๋ค.