当前位置: 动力学知识库 > 问答 > 编程问答 >

c# - Can enum represent string numbers?

问题描述:

I want a user to input "Sun" and have that represent "1".

Console.Write("Enter a Day: ");

day = Console.ReadLine();

can enum do this?

网友答案:

Yes.

enum Days { Sun = 1, /*...*/ }

string day = Console.ReadLine();

Days d = (Days)Enum.Parse(typeof(Days), day);
网友答案:

Yes - you can use Enum.Parse (and Enum.TryParse as of .NET 4) to parse a string to an enum value:

Day day = (Day) Enum.Parse(typeof(Day), "Sun");

or

Day day;
if (Enum.TryParse<Day>("Sun", out day))
{
    // Success!
}

It's still somewhat ugly, mind you - there's a certain amount of casting involved in the first call, and the second wouldn't stop you from trying to parse to any value type.

The final reason I don't really like Parse/TryParse is that they will parse string representations of the numeric values of the enums - so "1" would successfully parse, even though it's clearly not the name of an enum value. That seems a bit clunky to me.

There's a nicer (IMO!) approach if you use Unconstrained Melody, a small open source project I wrote to allow generics with enum constraints. Then you'd have:

Day day = Enums.ParseName<Day>("Sun");

or

Day day;
if (Enums.TryParseName<Day>("Sun", out day))
{
    // Success!
}

No casting, and you can't accidentally use it on a non-enum type. (There are various other goodies in the project, of course... description parsing etc.)

网友答案:

If you have an enum, lets say:

enum SkyObjects
{
   Sun = 1,
   Star = 2
}

You can then have the following:

SkyObjects day = Enum.Parse(typeof(SkyObjects ), Console.ReadLine());

This will have Sun, represented as 1 as you like.

网友答案:

or,... another candidate for an immutable value type struct that "appears as" and "behaves" like an enumeration, but has additional fuinctionality as required by your domain model concerns... Using the type defined below you can write

 var myDay = (InPutDay)"Sunday"; 

or, ...

 int x = (int)InPutDay.Sun;

or,

int x = (int) InputDay.Parse("Sunday");

Use this type:

   public struct InPutDay
   {
       private int val;
       private bool isDef;
       private InPutDay( )  { } // private to prevent direct instantiation 
       private InPutDay(int value) { id=value; isDef = true; } 

       public bool HasValue { get { return isDef; } } 
       public bool isNull{ get { return !isDef; } } 

       public static InPutDay Null = new InPutDay(); 
       public static InPutDay Sun = new InPutDay(1); 
       public static InPutDay Mon = new InPutDay(2); 
       public static InPutDay Tue = new InPutDay(3); 
       public static InPutDay Wed = new InPutDay(4); 
       public static InPutDay Thu = new InPutDay(5); 
       public static InPutDay Fri = new InPutDay(6); 
       public static InPutDay Sat = new InPutDay(7); 

       public static InPutDay Parse(string s)
       {
           switch (s.ToUpper().Substring(0,3))
           {
               case "SUN": return InPutDay.Sun;
               case "MON": return InPutDay.Mon;
               case "TUE": return InPutDay.Tue;
               case "WED": return InPutDay.Wed;
               case "THU": return InPutDay.Thu;
               case "FRI": return InPutDay.Fri;
               case "SAT": return InPutDay.Sat;
               default return InPutDay.Null;
           }
       }
       public static implicit operator int(InPutDay inDay)
       { return val; }
       public static explicit operator InPutDay (int inDay)
       { 
           if (inDay > 0 && inDay < 8)
              return new InPutDay(inDay);
           // either throw exception ....
           throw new ArgumentException(
               "InPutDay integer values must be between 1 and 7.");
           // or return null instance
           return InPutDay.Null;
       }
       public static explicit operator InPutDay (string inDayName)
       {  return InPutDay.Parse(inDayName); }               
  }
分享给朋友:
您可能感兴趣的文章:
随机阅读: