C#에서 열거 형을 문자열로 변환

이 자습서에서는 열거 형을 C#의 문자열로 변환하는 방법에 대해 설명합니다.
C#에서Description
특성을 사용하여 열거 형을 문자열로 변환
명명 규칙을 따르는 간단한 Enum 값의 문자열로 변환하기 위해 아무것도 사용할 필요가 없습니다. C#에서Console.WriteLine()
함수를 사용하여 사용자에게 표시 할 수 있습니다. 아래 코딩 예제에서 설명합니다.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace enum_to_string
{
public enum Status
{
InProgress,
Completed
}
class Program
{
static void Main(string[] args)
{
Status complete = Status.Completed;
Console.WriteLine(complete);
}
}
}
출력:
Completed
위의 코드에서 우리는 C#의 문자열 형식으로 Enum 값Completed
를 직접 인쇄했습니다. 이는 Enum 값이 C#의 변수 명명 규칙을 따랐기 때문에 가능했습니다. 그러나 독자에게 친숙한 문자열을 표시하려면 C#에서 Enums의Description
속성을 사용해야합니다. Description
속성은 Enum의 각 값을 설명하는 데 사용됩니다. Enum의Description
속성에 문자열을 작성하여 Enum을 판독기 친화적 인 문자열로 변환 할 수 있습니다. 다음 코드 예제는 C#에서Description
속성을 사용하여 Enum 값을 문자열로 변환하는 방법을 보여줍니다.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Reflection;
namespace enum_to_string
{
public enum Status
{
[Description("The Desired Task is InProgress")]
InProgress,
[Description("The Desired Task is Successfully Completed")]
Completed
}
static class extensionClass
{
public static string getDescription(this Enum e)
{
Type eType = e.GetType();
string eName = Enum.GetName(eType, e);
if (eName != null)
{
FieldInfo fieldInfo = eType.GetField(eName);
if (fieldInfo != null)
{
DescriptionAttribute descriptionAttribute =
Attribute.GetCustomAttribute(fieldInfo,
typeof(DescriptionAttribute)) as DescriptionAttribute;
if (descriptionAttribute != null)
{
return descriptionAttribute.Description;
}
}
}
return null;
}
}
class Program
{
static void Main(string[] args)
{
Status complete = Status.Completed;
string description = complete.getDescription();
Console.WriteLine(description);
}
}
}
출력:
The Desired Task is Successfully Completed
위의 코드에서 우리는 C#에서 Enum 값 설명을 반환하는 확장 메서드getDescription
을 만들었습니다. 이 방법은 완벽하게 작동하지만 약간 복잡합니다. 이 복잡성은 다음 섹션에서 단순화됩니다.
C#에서switch
문을 사용하여 Enum을 문자열로 변환
C#에서switch
문을 사용하면 이전 방법의 많은 복잡성을 단순화 할 수 있습니다. C#의switch
문을 사용하여 각 Enum 값의 문자열 변수에 원하는 값을 할당 할 수 있습니다. 다음 코드 예제를 참조하십시오.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Reflection;
namespace enum_to_string
{
public enum Status
{
InProgress,
Completed
}
static class extensionClass
{
public static string getValue(this Status e)
{
switch (e)
{
case Status.InProgress:
return "The Desired Task is InProgress";
case Status.Completed:
return "The Desired Task is Successfully Completed";
}
return String.Empty;
}
}
class Program
{
static void Main(string[] args)
{
Status complete = Status.Completed;
string value = complete.getValue();
Console.WriteLine(value);
}
}
}
출력:
The Desired Task is Successfully Completed
위의 코드에서는 C#의switch
문을 사용하여 Enum 값을 기반으로 문자열을 반환하는 확장 메서드getValue()
를 만들었습니다. getValue()
함수는switch
문을 사용하고 지정된 Enum의 각 값에 대해 다른 문자열을 반환합니다.
Maisam is a highly skilled and motivated Data Scientist. He has over 4 years of experience with Python programming language. He loves solving complex problems and sharing his results on the internet.
LinkedIn관련 문장 - Csharp Enum
관련 문장 - Csharp String
- C# Int를 문자열로 변환
- C# 문자열을 열거 형으로 변환
- C#의 switch 문에서 문자열 사용
- C#에서 문자열을 부울로 변환하는 방법
- C#에서 문자열을 부동 소수점으로 변환하는 방법
- C#에서 문자열을 바이트 배열로 변환하는 방법