닷넷 프레임워크
enum 데이터 타입에 null 할당하기
K :)
2010. 6. 24. 11:39
프로그램을 하다 보면 enum이나 sturct 데이터형에 null을 넣어서 초기화를 하고 싶을 때가 있다.
이 방법을 알기전까진 -9999이나 0처럼 쓰이지 않는 값을 NONE에 넣어서 초기화를 했는데, 그럴 필요가 없다.
모르면 손발이 고생.
C#에서 Value형태의 데이터형에 null 데이터를 넣을 수 있는 방법을 제공한다.
이 방법은 .net framework 2.0이상에서만 사용할 수 있다.
이 방법을 알기전까진 -9999이나 0처럼 쓰이지 않는 값을 NONE에 넣어서 초기화를 했는데, 그럴 필요가 없다.
모르면 손발이 고생.
C#에서 Value형태의 데이터형에 null 데이터를 넣을 수 있는 방법을 제공한다.
이 방법은 .net framework 2.0이상에서만 사용할 수 있다.
enum Color
{
RED = 0x39,
GREEN,
BLUE,
}
Nullable<Color> whatColor = new Color();
//whatColor = Color.GREEN;
whatColor = null;
if (whatColor != null)
{
Console.WriteLine(whatColor);
Console.WriteLine(Enum.Format(typeof(Color), whatColor, "g"));
Console.WriteLine(Enum.Format(typeof(Color), whatColor, "G"));
}
else
{
Console.WriteLine("whatColor variable is null.");
}
{
RED = 0x39,
GREEN,
BLUE,
}
Nullable<Color> whatColor = new Color();
//whatColor = Color.GREEN;
whatColor = null;
if (whatColor != null)
{
Console.WriteLine(whatColor);
Console.WriteLine(Enum.Format(typeof(Color), whatColor, "g"));
Console.WriteLine(Enum.Format(typeof(Color), whatColor, "G"));
}
else
{
Console.WriteLine("whatColor variable is null.");
}