C#根据枚举的数值(Value)获取对应的Name值

来源:互联网 发布:余弦匹配算法 编辑:程序博客网 时间:2024/06/05 00:20

原文链接:http://2sharings.com/2014/c-sharp-get-name-value-by-int
以前C#程序开发中,在获取枚举对应的Name时最常想到的方法是:传入枚举的value值,用switch或者if语句来逐个判断,然后取出匹配的Name值。但今天在这里给大家介绍一种更简洁的实现方式,用Enum.GetName()方法,具体实现看以下代码:

using System;namespace JsonDynamic{  class Program  {    static void Main(string[] args)    {      Console.WriteLine("请输入一个0-3的数字...");      //Console.ReadKey();      var input = Convert.ToInt32(Console.ReadLine());      Console.WriteLine("数字{0}对应的枚举Name值:{1}", input, Enum.GetName(typeof(SocialType), input));      Console.ReadKey();    }  }  enum SocialType : int  {    Facebook,    Twitter,    GooglePlus,    Other  }}
阅读全文
0 0
原创粉丝点击