首页 技术 正文
技术 2022年11月6日
0 收藏 997 点赞 323 浏览 2071 个字

简介

1. 枚举(enum type)通常用来表示一组常量。由于枚举是强类型的,这在编程中给我们提供了极大的方便。

2. 枚举的定义:

 public enum Sex
{
男 = 0,
女 = 1
}

或者:如果只给男赋值,那么女=1

 public enum Sex
{
男 = 0,

}

枚举在软件开发中的使用场景

在数据库设计人员表(person)时有性别字段Sex(0代表男,1代表女),我们一般用bit或者int类型表示。

1.在编程时我们给Sex字段赋值的方式为:

1).  Sex=0;

2).  Sex=(int)SexEnum.Man;

其中SexEnum为定义性别的枚举类型,我们可以看出第二种方式的可读性更强。

2.在编程时我们,如果Sex字段作为一个搜索条件的话,我们可能需要以下拉选择的方式展现所有可以选择的情况。那么我们就需要将SexEnum转换成一个字典集合然后绑定到对应的select标签,具体怎么实现请看下面的示例代码。

………………………………

enum、int、string三种类型之间的互转

c#枚举使用详解

执行结果如下:

c#枚举使用详解

获取描述信息

修改枚举如下:

c#枚举使用详解

获取描述信息代码如下:

c#枚举使用详解

打印结果如下:

c#枚举使用详解

枚举转换成字典集合的通用方法

1.这里我就直接列举代码如下:

 public static class EnumHelper
{
/// <summary>
/// 根据枚举的值获取枚举名称
/// </summary>
/// <typeparam name="T">枚举类型</typeparam>
/// <param name="status">枚举的值</param>
/// <returns></returns>
public static string GetEnumName<T>(this int status)
{
return Enum.GetName(typeof(T), status);
}
/// <summary>
/// 获取枚举名称集合
/// </summary>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
public static string[] GetNamesArr<T>()
{
return Enum.GetNames(typeof(T));
}
/// <summary>
/// 将枚举转换成字典集合
/// </summary>
/// <typeparam name="T">枚举类型</typeparam>
/// <returns></returns>
public static Dictionary<string, int> getEnumDic<T>()
{ Dictionary<string, int> resultList = new Dictionary<string, int>();
Type type = typeof(T);
var strList = GetNamesArr<T>().ToList();
foreach (string key in strList)
{
string val = Enum.Format(type, Enum.Parse(type, key), "d");
resultList.Add(key, int.Parse(val));
}
return resultList;
}
/// <summary>
/// 将枚举转换成字典
/// </summary>
/// <typeparam name="TEnum"></typeparam>
/// <returns></returns>
public static Dictionary<string, int> GetDic<TEnum>()
{
Dictionary<string, int> dic = new Dictionary<string, int>();
Type t = typeof(TEnum);
var arr = Enum.GetValues(t);
foreach (var item in arr)
{
dic.Add(item.ToString(), (int)item);
} return dic;
} }
  public enum Sex
{
man,
woman
}
public enum Color
{
red,
blue
}

使用方法如下:

 static void Main(string[] args)
{
var name = EnumHelper.GetEnumName<Sex>();
Console.WriteLine("数字转字符串:"+name);
var dic1 = EnumHelper.getEnumDic<Sex>();
Console.WriteLine("枚举转字典集合方法1:");
foreach (var item in dic1)
{
Console.WriteLine(item.Key + "==" + item.Value);
}
Console.WriteLine("枚举转字典集合方法2:");
var dic= EnumHelper.GetDic<Color>();
foreach (var item in dic)
{
Console.WriteLine(item.Key+"=="+item.Value);
}
Console.ReadLine();
}

c#枚举使用详解

 
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,088
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,564
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,412
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,185
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,822
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,905