1. enum 通常定义在namespace 中,这样,所有在这个namespace下的类都可以访问这个enum。
enum 也可以定义在class 或struct 中。
- Enum常与switch连用
- 枚举格式字符串
Console.WriteLine(ConsoleColor.Red.ToString(“G”)); // Displays Red
Console.WriteLine(ConsoleColor.Red.ToString(“D”)); //十进制
Console.WriteLine(ConsoleColor.Red.ToString(“F”)); // Displays Red
Console.WriteLine(ConsoleColor.Red.ToString(“X”)); //displays hex
- 代码示例
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
namespace testEnum
{
public enum EAlarmType : uint
{
Null = 0,
Common = 1,
Warn = 2,
Serious = 3,
}
public enum MsgDetails : uint
{
Null = 0,
PLC_Error = 1,
GPIO_Error = 2,
PC_PowerOff = 3,
Reader_Error = 4
}
public class EnumComparer<T> : IEqualityComparer<T> where T : struct
{
public bool Equals(T first, T second)
{
var firstPara = Expression.Parameter(typeof(T), "first");
var secondPara = Expression.Parameter(typeof(T), "second");
var equalExpression = Expression.Equal(firstPara, secondPara);
return Expression.Lambda<Func<T, T, bool>>(equalExpression, new[] { firstPara, secondPara }).Compile().Invoke(first, second);
}
public int GetHashCode(T instance)
{
var parameter = Expression.Parameter(typeof(T), "instance");
var convertExpression = Expression.Convert(parameter, typeof(int));
return Expression.Lambda<Func<T, int>>(convertExpression, new[] { parameter }).Compile().Invoke(instance);
}
}
class Program
{
static void Main(string[] args)
{
//获取枚举项的名字---Enum.GetName//
string getname = Enum.GetName(typeof(MsgDetails), MsgDetails.GPIO_Error);
Console.WriteLine("{0}--{1}",MsgDetails.GPIO_Error,getname);
//将string/常量值--转为枚举项 Enum.Parse
MsgDetails msgMember = (MsgDetails)Enum.Parse(typeof(MsgDetails), getname);
///获取枚举项对应的常量值
int value = (int)MsgDetails.PLC_Error;
Console.WriteLine("{0}--{1}",MsgDetails.PLC_Error,value);
//依据常量值获取枚举中对应项---Enum.ToObject
MsgDetails fhkg = (MsgDetails)Enum.ToObject(typeof(MsgDetails), value);
//判断枚举中是否包含某一项-- Enum.IsDefined
bool IsDefined = Enum.IsDefined(typeof(MsgDetails), getname);
Console.WriteLine(IsDefined);
//Enum类型没有实现IEquatable接口,Dictionary中无法添加Enum类型的元素
Dictionary<string, string> dic = new Dictionary<string, string>();
dic.Add(Enum.GetName(typeof(MsgDetails), MsgDetails.GPIO_Error), "common");
dic.Add(Enum.GetName(typeof(MsgDetails), MsgDetails.PLC_Error), "servier");
//MsgDetails.PLC_Error.ToString()--- Enum.GetName(typeof(MsgDetails), MsgDetails.GPIO_Error)都可以获取常量值对应的名字
string etype = dic[ Enum.GetName(typeof(MsgDetails), MsgDetails.GPIO_Error)];
Console.WriteLine("key--{0}, value--{1}",MsgDetails.GPIO_Error, etype);
Console.WriteLine("Key--{0},value--{1}", MsgDetails.PLC_Error, dic[MsgDetails.PLC_Error.ToString()]);
//通过实现Enum的IEquatable接口,便可以将enum类型作为key 添加到dictionary中
var dicT = new Dictionary<MsgDetails, EAlarmType>(new EnumComparer<MsgDetails>());
dicT.Add(MsgDetails.GPIO_Error, EAlarmType.Common);
dicT.Add(MsgDetails.PC_PowerOff, EAlarmType.Serious);
dicT.Add(MsgDetails.PLC_Error, EAlarmType.Common);
//获取dictionary中key对应的value值
EAlarmType type = dicT[MsgDetails.PC_PowerOff];
Console.WriteLine(type);
Console.ReadLine();
}
}
}