在枚举方面需要帮助 [英] need help on Enumerations

查看:84
本文介绍了在枚举方面需要帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好...

hi all...

namespace EnumExample
{
    class Program
    {
        public enum TimeOfDay
        {
            Morning = 0,
            Afternoon = 1,
            Evening = 2
        }
        static int Main(string[] args)
        {
            WriteGreeting(TimeOfDay.Morning);
            return 0;
        }
        static void WriteGreeting(TimeOfDay timeOfDay)
        {
            // the output through switch case is "Good morning!"
             switch(timeOfDay)
             {
                 case TimeOfDay.Morning:
                     Console.WriteLine("Good morning!");
                     break;
                 case TimeOfDay.Afternoon:
                     Console.WriteLine("Good afternoon!");
                     break;
                 case TimeOfDay.Evening:
                     Console.WriteLine("Good evening!");
                     break;
                 default:
                     Console.WriteLine("Hello!");
                     break;
             }
            
            // the output for following two lines is "Afternoon"
           
             TimeOfDay time = TimeOfDay.Afternoon;
             Console.WriteLine(time.ToString());            // ToString() method is used
            
            // the output of following two lines also "Afternoon"
           
             TimeOfDay time3 = TimeOfDay.Afternoon;
             Console.WriteLine(time3);                      // the same output is coming even the ToString() method was not used


            //=============================================================
            
                     
   // the output for following two lines is "2"
            
    TimeOfDay time2 = (TimeOfDay)Enum.Parse(typeof(TimeOfDay), "evening", true); // Enum.Parse method is used
             Console.WriteLine((int)time2);
          
 // the output of following two lines also "2"
          
  TimeOfDay time4 = TimeOfDay.Evening; // the same output is coming even the Enum.Parse method was not used
       
   Console.WriteLine((int)time4);



        }

    }
}




现在我的疑问是...
1)在不使用ToString()方法的情况下,我也得到了相同的输出"Afternoon".
2)不使用Enum.Parse()方法,我也将获得相同的输出"2".

那我可以使用这两种方法的情况是什么呢?

请给我建议这个话题.....

谢谢




now my doubt is...
1) without using ToString() method also i''m getting same output "Afternoon".
2) without using Enum.Parse() method also i''m getiing same output "2".

then what are the cases that i can use this two methods???

please suggest me in this topic.....

thank you

推荐答案

这是因为,即使您没有显式调用.ToString(),在第一种情况下也会隐式调用ToString()方法.

并且,仅当需要将字符串值转换为Enum表示形式时,才需要 Enum.Parse().在您的情况下,由于您已经有一个枚举(TimeOfDay.Evening,在内部为其分配了一个int值),您将获得相应的int值输出.
It''s because, the ToString() method is implicitly being called in first cases, even if you don''t call .ToString() explicitly.

And, the Enum.Parse() is required only if you need to convert a string value to an Enum representation. In your case, as you already have an Enum (TimeOfDay.Evening, which is internally assigned an int value), you will get the corresponding int value output.


在在需要字符串的上下文中,框架会自动调用其ToString()方法,即使您没有显式地执行它也是如此.
在代码中,如果将枚举值强制转换为int,则框架将调用Int32类的ToString()方法;否则,框架将调用Int32类的ToString()方法.这就是为什么在某些情况下您得到2作为输出的原因.
When you use a variable in a context where a string is required, the framework automatically calls its ToString() method, even if you don''t do it explicitly.
In your code, if you cast the enumerated value to an int, then the framework calls the ToString() method of the Int32 class; this is why in some cases you get 2 as output.


这篇关于在枚举方面需要帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆