Is it possible to list the number of lines?

I want the user to enter "Sun" and represent "1".

Console.Write("Enter a Day: ");
day = Console.ReadLine();

can enum do this?

+3
source share
4 answers

Yes.

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

string day = Console.ReadLine();

Days d = (Days)Enum.Parse(typeof(Days), day);
+8
source

Yes - you can use Enum.Parse(and Enum.TryParsewith .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!
}

This is still somewhat ugly, mind you - the first call involves a certain amount of casting, and the second does not stop you from trying to parse any type of value.

, Parse/TryParse, , - "1" , . .

(IMO!) , Unconstrained Melody - , , , :

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

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

, , . ( , , ,... ..)

+4

enum, :

enum SkyObjects
{
   Sun = 1,
   Star = 2
}

:

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

Sun, 1, .

+1

or, ... another candidate for a structure of an immutable type of type, which is "displayed as" and "behaves" as an enumeration, but has additional functionality, as required by your domain model model ... Using the type indicated 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); }               
  }
0
source

Source: https://habr.com/ru/post/1747428/


All Articles