{"id":1616,"date":"2005-08-24T09:33:00","date_gmt":"2005-08-24T09:33:00","guid":{"rendered":"http:\/\/t.motd.kr\/articles\/1616\/c-nullable-types"},"modified":"2022-12-28T01:48:32","modified_gmt":"2022-12-27T16:48:32","slug":"c-%ec%9d%98-nullable-types","status":"publish","type":"post","link":"https:\/\/vault.motd.kr\/wordpress\/posts\/1616\/c-%ec%9d%98-nullable-types\/","title":{"rendered":"C# \uc758 Nullable Types"},"content":{"rendered":"\n
\ud504\ub85c\uadf8\ub798\ubc0d\uc744 \ud558\ub2e4 \ubcf4\uba74 \uc131\uac00\uc2e0 \uac83 \uc911 \ud558\ub098\uac00\u00a0 \uc798\ubabb\ub41c \ub370\uc774\ud130\uac00 \ub4e4\uc5b4\uc654\uc744 \uacbd\uc6b0 \ucde8\ud560 \uc218 \uc788\ub294 \ud589\ub3d9\uc740<\/p>\n\n\n\n \uc815\ub3c4\uac00 \uc788\uaca0\uc2b5\ub2c8\ub2e4.<\/p>\n\n\n\n \uadf8\ub7f0\ub370, \uc774\ubc88 Visual Studio 2005 \uc5d0\uc11c \ub4f1\uc7a5\ud55c C# \uc758\u00a0 \ud55c\ud3b8\uc73c\ub85c\ub294 \ubb3c\uc74c\ud45c \ud558\ub098 \ub298\uc5b4\ub0ac\uc744 \ubfd0\uc774\uc9c0\ub9cc \uc5b8\uc5b4\uac00 \uc810\uc810 \ub354 \ubcf5\uc7a1\ud574 \uc9c0\uace0 \uc788\ub294 \uac83\uc740 \uc544\ub2cc\uac00 \uc2f6\uc740 \uc0dd\uac01\ub3c4 \ub4ed\ub2c8\ub2e4. \ud558\uc9c0\ub9cc \uc2dc\uac04\uc774 \uc9c0\ub0a0 \uc218\ub85d \uc5b8\uc5b4\uc758 \uad6c\uc131 \uc694\uc18c\uac00 \uc704\uc640 \uac19\uc774 API \uc640 \uacb0\ud569\ud558\uc5ec \ub354 \uac15\ub825\ud55c \ud6a8\uacfc\ub97c \ub0b4\ub294 \uac83\uc774 \uc694\uc998\uc758 \ucd94\uc138\uc774\uace0, \uadf8\uc5d0 \ub530\ub77c \uc5b8\uc5b4\uc640 API \uc758 \uacbd\uacc4\uac00 \ubaa8\ud638\ud574\uace0 \uc788\uc2b5\ub2c8\ub2e4. \ub0a8\uc774 \uc791\uc131\ud55c \ucf54\ub4dc\ub97c \uc77d\ub2e4\uac00 \ubaa8\ub974\ub294 API \uac00 \ub098\uc624\uba74 \uc774\ud574\ud558\uae30 \uc704\ud574 \ud559\uc2b5\ud574\uc57c \ud558\ub4ef, \uc774\ub7f0 \uc0c8\ub85c\uc6b4 \uc5b8\uc5b4 \uc694\uc18c\ub4e4\ub3c4 \ub9c8\ucc2c\uac00\uc9c0\uac00 \uc544\ub2cc\uac00 \uc0dd\uac01\ud574 \ubd05\ub2c8\ub2e4.<\/p>\n","protected":false},"excerpt":{"rendered":" \ud504\ub85c\uadf8\ub798\ubc0d\uc744 \ud558\ub2e4 \ubcf4\uba74 \uc131\uac00\uc2e0 \uac83 \uc911 \ud558\ub098\uac00\u00a0null\uc774 \ub098 \ubc94\uc704\ub97c \ubc97\uc5b4\ub09c \ub370\uc774\ud130\uc640 \uac19\uc740 \uc798\ubabb\ub41c \ub370\uc774\ud130\uc758 \ucc98\ub9ac\uc785\ub2c8\ub2e4. \uc800 \uac19\uc740 \uacbd\uc6b0\u00a0Jakarta Commons Lang\uc774\ub098 TL-Util, \uadf8\ub3c4 \uc544\ub2c8\uba74 \uadf8\ub0e5 \uc9c1\uc811 \uc77c\uc77c\uc774 \ucf54\ub529\ud558\uace4 \ud569\ub2c8\ub2e4. \uc798\ubabb\ub41c \ub370\uc774\ud130\uac00 \ub4e4\uc5b4\uc654\uc744 \uacbd\uc6b0 \ucde8\ud560 \uc218 \uc788\ub294 \ud589\ub3d9\uc740 \uc815\ub3c4\uac00 \uc788\uaca0\uc2b5\ub2c8\ub2e4. \uadf8\ub7f0\ub370, \uc774\ubc88 Visual Studio 2005 \uc5d0\uc11c \ub4f1\uc7a5\ud55c C# \uc758\u00a0Nullable\u00a0\ud0c0\uc785\uc740 \uc608\uc678\ub97c \ub358\uc9c0\uc9c0 \uc54a\uace0\u00a0null\u00a0\uac12\uc744 \uc801\uc808\ud788 \ucc98\ub9ac\ud560 \uc218 \uc788\ub3c4\ub85d \ub3d5\uace0… Continue reading null<\/code>\uc774 \ub098 \ubc94\uc704\ub97c \ubc97\uc5b4\ub09c \ub370\uc774\ud130\uc640 \uac19\uc740 \uc798\ubabb\ub41c \ub370\uc774\ud130\uc758 \ucc98\ub9ac\uc785\ub2c8\ub2e4. \uc800 \uac19\uc740 \uacbd\uc6b0\u00a0Jakarta Commons Lang<\/a>\uc774\ub098 TL-Util, \uadf8\ub3c4 \uc544\ub2c8\uba74 \uadf8\ub0e5 \uc9c1\uc811 \uc77c\uc77c\uc774 \ucf54\ub529\ud558\uace4 \ud569\ub2c8\ub2e4.<\/p>\n\n\n\n
\n
\n
NullPointerException<\/code> –
null<\/code> \uc778 \uacbd\uc6b0<\/li>\n\n\n\n
IllegalArgumentException<\/code> –
null<\/code>\uc740 \uc544\ub2c8\ub098 \uaddc\uc57d \uc704\ubc18\uc778 \uacbd\uc6b0<\/li>\n\n\n\n
RuntimeException<\/code> \uc0c1\uc18d<\/li>\n\n\n\n
AssertionError<\/code>\u00a0–\u00a0
assert<\/code>\u00a0\ubb38 \uc0ac\uc6a9\uc2dc\n
\n
Nullable<\/code>\u00a0\ud0c0\uc785\uc740 \uc608\uc678\ub97c \ub358\uc9c0\uc9c0 \uc54a\uace0\u00a0
null<\/code>\u00a0\uac12\uc744 \uc801\uc808\ud788 \ucc98\ub9ac\ud560 \uc218 \uc788\ub3c4\ub85d \ub3d5\uace0 \uc788\uc2b5\ub2c8\ub2e4. \ubaa8\ub4e0 \ud0c0\uc785 \uc120\uc5b8\uc740 \ud0c0\uc785\uba85 \ub4a4\uc5d0\u00a0
?<\/code>\u00a0\uc624\ud37c\ub808\uc774\ud130\ub97c \ubd99\uc5ec\u00a0
Nullable<\/code>\u00a0\uc774 \ub420 \uc218 \uc788\uc2b5\ub2c8\ub2e4:<\/p>\n\n\n\n
int? x = 123;\r\nint? y = null;\r\nif (x.HasValue) Console.WriteLine(x.Value); \/\/ Printed\r\nif (y.HasValue) Console.WriteLine(y.Value); \/\/ Not Printed<\/code><\/pre>\n\n\n\n
Nullable<\/code> \uacfc \uc77c\ubc18 \ud0c0\uc785 \uac04\uc758 \ubcc0\ud658\uc740 \uc790\uc720\ub86d\uc2b5\ub2c8\ub2e4:<\/p>\n\n\n\n
int i = 123;\nint? x = i; \/\/ int \u2013> int?\ndouble? y = x; \/\/ int? \u2013> double?\nint? z = (int?)y; \/\/ double? \u2013> int?\n\n\/\/ DOWNCAST: Throws exception if z is null.\nint j = (int)z; \/\/ int? \u2013> int<\/code><\/pre>\n\n\n\n
Nullable<\/code> \uac04 \uc5f0\uc0b0\uc740 \ub9e4\uc6b0 \uc720\uc6a9\ud574 \ubcf4\uc785\ub2c8\ub2e4:<\/p>\n\n\n\n
int? x = GetNullableInt();\nint? y = GetNullableInt();\n\n\/\/ Two statements do the same job.\nint? z = x.HasValue && y.HasValue ? x.Value + y.Value : (int?)null;\nint? z = x + y;<\/code><\/pre>\n\n\n\n
??<\/code> \uc5f0\uc0b0\uc790\ub97c \uc774\uc6a9\ud55c \ub514\ud3f4\ud2b8 \uac12 \uce58\ud658\uc740 \uc190\uac00\ub77d\uc744 \ub35c \ud53c\uace4\ud558\uac8c \ud558\uaca0\uad70\uc694:<\/p>\n\n\n\n
string s = GetStringValue();\n\n\/\/ Print \u2018Unspecified\u2019 if s is null\nConsole.WriteLine(s ?? \u201cUnspecified\u201d);<\/code><\/pre>\n\n\n\n