C# enum to string auto-conversion?
Asked Answered
B

6

17

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
Biotechnology answered 9/6, 2010 at 23:11 Comment(4)
Unfortunately, programming involves a lot of typing, so you're just going to have to get used to it.Worsley
Hmm not sure about on an enum but on a class you could use an implicit operator. msdn.microsoft.com/en-us/library/85w54y0a.aspxProlamine
@codeka: Your response is funny. Dunno about you but I started coding for the sole fact that I am lazy and wanted to make using the computer easier (ie: shell script automation, simple batch files, etc.). It's a perfectly valid question to ask how to make something easier.Prolamine
@Cory: there's being lazy and then there's being lazy. It's good to write shell scripts to make your life easier, but it's bad to circumvent the type system to save typing 11 characters.Worsley
G
11

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);
Gerardgerardo answered 9/6, 2010 at 23:16 Comment(4)
Ok, just wishful thinking on my part.Biotechnology
Also, just to be clear, there's nothing special about Console.WriteLine, it just has an overload that takes an Object and the enum is getting boxed, passed as an object and then WriteLine calls ToString on it.Worsley
@Biotechnology - if you're curious, it is possible to write your own implicit conversion operator for your enum to a string! csharphelp.com/2006/10/…Utmost
Perhaps I'm missing something but I actually don't think it is possible to write your own implicit conversion, enum is not a class so you cant add methods to it.Archway
M
11

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";
}
Morvin answered 10/2, 2017 at 7:52 Comment(1)
The issue with this solution is that the type that is getting passed around is a string. The caller can still pass in any string that they want.Larkins
U
1

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".
Utmost answered 9/6, 2010 at 23:17 Comment(0)
I
1

The correct syntax should be

myRank.ToString("F");
Icy answered 9/6, 2010 at 23:46 Comment(0)
E
0

[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()

Exequatur answered 14/7, 2015 at 13:15 Comment(0)
C
-1

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);
Concave answered 27/11, 2023 at 12:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.