在c#中,是否有可能用属性装饰Enum类型或做一些其他事情来指定默认值应该是什么,而不改变值?由于某种原因,所需的数字可能是固定不变的,而且仍然可以控制默认值是很方便的。

enum Orientation
{
    None = -1,
    North = 0,
    East = 1,
    South = 2,
    West = 3
}

Orientation o; // Is 'North' by default.

当前回答

任何enum的默认值为0。因此,如果您想将一个枚举数设置为默认值,则将该枚举数设置为零,并将所有其他枚举数设置为非零(如果有多个枚举数的值为零,则第一个值为零的枚举数将是该枚举的默认值)。

enum Orientation
{
    None = 0, //default value since it has the value '0'
    North = 1,
    East = 2,
    South = 3,
    West = 4
}

Orientation o; // initialized to 'None'

如果枚举数不需要显式值,则只需确保第一个枚举数是您希望作为默认枚举数的枚举数,因为“默认情况下,第一个枚举数的值为0,并且每个后续枚举数的值都加1。”(c#引用)

enum Orientation
{
    None, //default value since it is the first enumerator
    North,
    East,
    South,
    West
}

Orientation o; // initialized to 'None'

其他回答

你不能,但如果你想的话,你可以耍点小把戏。:)

    public struct Orientation
    {
        ...
        public static Orientation None = -1;
        public static Orientation North = 0;
        public static Orientation East = 1;
        public static Orientation South = 2;
        public static Orientation West = 3;
    }

此结构体的用法为简单enum。 你可以创建p.a == Orientation。默认为East(或您想要的任何值) 要使用这个技巧本身,您需要通过代码从int转换。 实现如下:

        #region ConvertingToEnum
        private int val;
        static Dictionary<int, string> dict = null;

        public Orientation(int val)
        {
            this.val = val;
        }

        public static implicit operator Orientation(int value)
        {
            return new Orientation(value - 1);
        }

        public static bool operator ==(Orientation a, Orientation b)
        {
            return a.val == b.val;
        }

        public static bool operator !=(Orientation a, Orientation b)
        {
            return a.val != b.val;
        }

        public override string ToString()
        {
            if (dict == null)
                InitializeDict();
            if (dict.ContainsKey(val))
                return dict[val];
            return val.ToString();
        }

        private void InitializeDict()
        {
            dict = new Dictionary<int, string>();
            foreach (var fields in GetType().GetFields(BindingFlags.Public | BindingFlags.Static))
            {
                dict.Add(((Orientation)fields.GetValue(null)).val, fields.Name);
            }
        } 
        #endregion

任何enum的默认值为0。因此,如果您想将一个枚举数设置为默认值,则将该枚举数设置为零,并将所有其他枚举数设置为非零(如果有多个枚举数的值为零,则第一个值为零的枚举数将是该枚举的默认值)。

enum Orientation
{
    None = 0, //default value since it has the value '0'
    North = 1,
    East = 2,
    South = 3,
    West = 4
}

Orientation o; // initialized to 'None'

如果枚举数不需要显式值,则只需确保第一个枚举数是您希望作为默认枚举数的枚举数,因为“默认情况下,第一个枚举数的值为0,并且每个后续枚举数的值都加1。”(c#引用)

enum Orientation
{
    None, //default value since it is the first enumerator
    North,
    East,
    South,
    West
}

Orientation o; // initialized to 'None'

enum的默认值是等于0的枚举成员或第一个元素(如果没有指定value)…但我在我的项目中使用枚举遇到了严重的问题,并通过做下面的事情来克服…无论我的需求是如何与阶级水平相关的……

    class CDDtype
    {
        public int Id { get; set; }
        public DDType DDType { get; set; }

        public CDDtype()
        {
            DDType = DDType.None;
        }
    }    


    [DefaultValue(None)]
    public enum DDType
    {       
        None = -1,       
        ON = 0,       
        FC = 1,       
        NC = 2,       
        CC = 3
    }

并得到预期的结果

    CDDtype d1= new CDDtype();
    CDDtype d2 = new CDDtype { Id = 50 };

    Console.Write(d1.DDType);//None
    Console.Write(d2.DDType);//None

现在,如果价值来自DB ....在这种情况下……将值传递给下面的函数,它会将值转换为enum…下面的函数处理了各种场景,它是通用的…相信我,这是非常快.....:)

    public static T ToEnum<T>(this object value)
    {
        //Checking value is null or DBNull
        if (!value.IsNull())
        {
            return (T)Enum.Parse(typeof(T), value.ToStringX());
        }

        //Returanable object
        object ValueToReturn = null;

        //First checking whether any 'DefaultValueAttribute' is present or not
        var DefaultAtt = (from a in typeof(T).CustomAttributes
                          where a.AttributeType == typeof(DefaultValueAttribute)
                          select a).FirstOrNull();

        //If DefaultAttributeValue is present
        if ((!DefaultAtt.IsNull()) && (DefaultAtt.ConstructorArguments.Count == 1))
        {
            ValueToReturn = DefaultAtt.ConstructorArguments[0].Value;
        }

        //If still no value found
        if (ValueToReturn.IsNull())
        {
            //Trying to get the very first property of that enum
            Array Values = Enum.GetValues(typeof(T));

            //getting very first member of this enum
            if (Values.Length > 0)
            {
                ValueToReturn = Values.GetValue(0);
            }
        }

        //If any result found
        if (!ValueToReturn.IsNull())
        {
            return (T)Enum.Parse(typeof(T), ValueToReturn.ToStringX());
        }

        return default(T);
    }

实际上,枚举的默认值是枚举中第一个值为0的元素。

例如:

public enum Animals
{
    Cat,
    Dog,
    Pony = 0,
}
//its value will actually be Cat not Pony unless you assign a non zero value to Cat.
Animals animal;

enum(实际上,任何值类型)的默认值是0——即使这不是该enum的有效值。这是无法改变的。