C# 枚举到字符串自动转换?

发布于 2024-09-04 19:50:26 字数 293 浏览 12 评论 0原文

是否可以让编译器自动将我的 Enum 值转换为字符串,这样我就可以避免每次都显式调用 ToString 方法。这是我想做的一个例子:

enum Rank { A, B, C }

Rank myRank = Rank.A;
string myString = Rank.A; // Error: Cannot implicitly convert type 'Rank' to 'string'
string myString2 = Rank.A.ToString(); // OK: but is extra work

Is it possible to have the compiler automatically convert my Enum values to strings so I can avoid explicitly calling the ToString method every time. Here's an example of what I'd like to do:

enum Rank { A, B, C }

Rank myRank = Rank.A;
string myString = Rank.A; // Error: Cannot implicitly convert type 'Rank' to 'string'
string myString2 = Rank.A.ToString(); // OK: but is extra work

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

只等公子 2024-09-11 19:50:26

不。枚举是它自己的类型,因此如果您想将其转换为其他类型,您必须做一些工作。

但是,根据您使用它执行的操作,某些方法会自动为您调用 ToString() 。例如,您可以这样做:

Console.Writeline(Rank.A);

No. An enum is its own type, so if you want to convert it to something else, you have to do some work.

However, depending on what you're doing with it, some methods will call ToString() on it automatically for you. For example, you can do:

Console.Writeline(Rank.A);
野生奥特曼 2024-09-11 19:50:26

您可能不是在寻找枚举本身,而是在寻找字符串常量列表。在某些场景下它可以更好地满足您的需求。

使用这个代替:

public static class Rank
{
   public const string A = "A";
   public const string B = "B";
   public const string C = "C";
}

You are not probably looking for enums itself, but a list of string constant. It can fit your needs better in some scenarios.

Use this instead:

public static class Rank
{
   public const string A = "A";
   public const string B = "B";
   public const string C = "C";
}
烟织青萝梦 2024-09-11 19:50:26

不,但至少当您可能需要使用其字符串值时,您可以使用枚举来调用其 ToString() 方法,例如:

Console.WriteLine(Rank.A); //prints "A".

No, but at least you can do things with enums that will call their ToString() methods when you might need to use their string value, e.g.:

Console.WriteLine(Rank.A); //prints "A".
我的鱼塘能养鲲 2024-09-11 19:50:26

正确的语法应该是

myRank.ToString("F");

The correct syntax should be

myRank.ToString("F");
空城缀染半城烟沙 2024-09-11 19:50:26

[警告,黑客] 不确定这是否令人讨厌,对我来说这似乎是一个合理的妥协。

var myEnumAsString = MyEnum+"";
Console.WriteLine(myEnumAsString); //我的枚举

这将强制隐式 ToString()

[Caution, hack] Unsure as to whether this is nasty, to me it seems a reasonable compromise.

var myEnumAsString = MyEnum+"";
Console.WriteLine(myEnumAsString); //MyEnum

This will force implicit ToString()

挽容 2024-09-11 19:50:26

这个问题很多年前就被问过,但是尽管如此......

这是从枚举中获取 string[] 的非常简单的方法:

public static string[] EnumToStr<T>() where T : Enum
{
    return Enum.GetNames(typeof(T));
}

只需像这样使用它:

public enum Test
{
    alpha,
    beta,
    gamma
}

var test_str = EnumToStr<Test>();

foreach (var name in test_str)
     Console.WriteLine(name);

This question was asked many years ago, but nevertheless...

Here's a very easy way to get a string[] from an enum:

public static string[] EnumToStr<T>() where T : Enum
{
    return Enum.GetNames(typeof(T));
}

Simply use it like this:

public enum Test
{
    alpha,
    beta,
    gamma
}

var test_str = EnumToStr<Test>();

foreach (var name in test_str)
     Console.WriteLine(name);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文