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
dcompiled
  • 4,762
  • 6
  • 33
  • 36
  • 15
    Unfortunately, programming involves a lot of typing, so you're just going to have to get used to it. – Dean Harding Jun 09 '10 at 23:15
  • Hmm not sure about on an enum but on a class you could use an implicit operator. http://msdn.microsoft.com/en-us/library/85w54y0a.aspx – Cory Charlton Jun 09 '10 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. – Cory Charlton Jun 09 '10 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. – Dean Harding Jun 09 '10 at 23:23

5 Answers5

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);
Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92
womp
  • 115,835
  • 26
  • 236
  • 269
  • Ok, just wishful thinking on my part. – dcompiled Jun 09 '10 at 23:30
  • 7
    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. – Dean Harding Jun 09 '10 at 23:32
  • 1
    @dcompiled - if you're curious, it is possible to write your own implicit conversion operator for your enum to a string! http://www.csharphelp.com/2006/10/type-conversion-and-conversion-operators-in-c/ – Mike Atlas Jun 09 '10 at 23:38
  • 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. – Robert Noack Feb 24 '15 at 00:06
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";
}
Tomas Kubes
  • 23,880
  • 18
  • 111
  • 148
  • 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. – Ryan Pedersen Apr 22 '23 at 20:32
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".
Mike Atlas
  • 8,193
  • 4
  • 46
  • 62
1

The correct syntax should be

myRank.ToString("F");
Syd
  • 1,526
  • 1
  • 15
  • 16
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()

Vix
  • 1,046
  • 10
  • 16