在C#中使用枚举时进行强制转换 [英] Casting while using enumerations in C#

查看:131
本文介绍了在C#中使用枚举时进行强制转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
       class Values
    {
        enum Temperatures
        {
            WickedCold=0,
            FreezingPoint=32,
            LightJacketWeather=60,
            SwimingWeather=72,
            BoilingPoint=212,
        }

        static void Main()
        {
            Console.WriteLine("Freezing Point of Water: {0}", (int)  Temperatures.FreezingPoint);
            Console.WriteLine("Boiling Point of Water: {0}", (int) Temperatures.BoilingPoint);
            Console.ReadLine();
        }
    }
}



我最近开始学习C#,但是我陷入了枚举的概念,即为什么默认情况下枚举是整数类型,为什么我们在显示输出时进行转换?
如果我删除(int),则输出为冻结水":FreezingPoint?请帮助



I have recently started learning C# i am stucked in the concept of enumeration that why we are casting when displaying the output although enumeration by default is of type integer?
If i remove (int) the output is Freezing of water is: FreezingPoint? Please help

推荐答案

枚举的默认输出使用枚举的ToString方法.您需要将一个emum强制转换为int,以使输出格式化程序知道您想要输出的int版本.
The default output for an enum uses the ToString method of an enum. You need to cast an emum to int to let the output formatter know that you want the int version of output.


只需转到http://www.c-sharpcorner.com/uploadfile/puranindia/enums-in-C-Sharp/ [ ^ ],或参见

http://csharp.net-tutorials.com/advanced/enumerations/ [
Just go to http://www.c-sharpcorner.com/uploadfile/puranindia/enums-in-C-Sharp/[^], or see

http://csharp.net-tutorials.com/advanced/enumerations/[^]


这篇关于在C#中使用枚举时进行强制转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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