2017년 2월 15일 수요일

[C#] Nullable object

Normal condition.

int i = 10;  // OK

but

int i = null;   // Compile error




To set a null to type of int variable, you can use int?.

int? i = null; // OK



To convert int and int?, do it following examples.

int i = 5;  // OK
int y = i; // Compile error
int y = (int)i;  // OK



To check if nullable type of int variable has a null, call HasValue property.

int? i = null;
if(i.HasValue == false)
{
   Console.WriteLine("null");
}

댓글 없음:

댓글 쓰기