18

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
4
  • 16
    Unfortunately, programming involves a lot of typing, so you're just going to have to get used to it. Commented Jun 9, 2010 at 23:15
  • Hmm not sure about on an enum but on a class you could use an implicit operator. msdn.microsoft.com/en-us/library/85w54y0a.aspx Commented Jun 9, 2010 at 23:17
  • 5
    @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. Commented Jun 9, 2010 at 23:21
  • 3
    @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. Commented Jun 9, 2010 at 23:23

7 Answers 7

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);
Sign up to request clarification or add additional context in comments.

4 Comments

Ok, just wishful thinking on my part.
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.
@dcompiled - if you're curious, it is possible to write your own implicit conversion operator for your enum to a string! csharphelp.com/2006/10/…
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.
10

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";
}

1 Comment

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.
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".

Comments

1

The correct syntax should be

myRank.ToString("F");

Comments

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

Comments

0

I know it is quite a late answer but nowadays you can do something like:

enum Rank { A, B, C }

public static class Ranks
{
        public const string A = nameof(Rank.A);
        public const string B = nameof(Rank.B);
        public const string C = nameof(Rank.C);
}

This is helpful because nameof is executed during compilation therefore you can use it as a const:

[Authorize(Roles = Ranks.A)]

Comments

-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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.