(C#) 38. Nullable (널러블)
Syntax (문법)
Nullable () : Syntax ()
- 최초 작성일: 2021년 3월 21일(월)
##
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
using System;
namespace nullable
{
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; // monster가 null인지 아닌지 확인해서 null이면 null, 아니면 뒤의 id를 넣음.
//아래의 내용과 같은 원리.
//if (monster == null) id = null;
//else id = monster.id;
// Nullable -> Null + able
int? number = 5; //null 도 될 수 잇다.
int c = (number != null) ? number.Value : 0;
int b = number ?? 0; // null 이면 0을 넣고, null 이 아니면 number를 b에 넣는다.
Console.WriteLine(b);
/*
if (number != null)
{
int a = number.Value;
Console.WriteLine(a);
}
if (number.HasValue)
{
int a = number.Value;
Console.WriteLine(a);
}
*/
//int a = number;
}
}
}
이 기사는 저작권자의 CC BY 4.0 라이센스를 따릅니다.