Choosing the default value of an Enum type without having to change values


The default value of any enum is zero. So if you want to set one enumerator to be the default value, then set that one to zero and all other enumerators to non-zero (the first enumerator to have the value zero will be the default value for that enum if there are several enumerators with the value zero).

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'

If your enumerators don't need explicit values, then just make sure the first enumerator is the one you want to be the default enumerator since "By default, the first enumerator has the value 0, and the value of each successive enumerator is increased by 1." (C# reference)

enum Orientation
    None, //default value since it is the first enumerator

Orientation o; // initialized to 'None'


In C#, is it possible to decorate an Enum type with an attribute or do something else to specify what the default value should be, without having the change the values? The numbers required might be set in stone for whatever reason, and it'd be handy to still have control over the default.

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

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

Html.EnumDropdownListFor: Showing a default text

Try to change the Index of LicenseTypes start from 1 not 0 like below:

public enum LicenseTypes
    Trial = 1,
    Paid = 2

Then you can use Range attribute to validate the selected license type like below:

public class YourViewModel
     //Other properties
     [Range(1,int.MaxValue,ErrorMessage = "Select a correct license")]
     public LicenseTypes LicenseTypes { get; set; }

Finally, in your view:

   @Html.EnumDropDownListFor(m => m.LicenseTypes,"Select a license",new { @class = "form-control"})
   @Html.ValidationMessageFor(m => m.LicenseTypes)

I have an enum:

public enum Sex

In my model I have:

    public Sex? Sex { get; set; }

An in the view:

    @Html.EnumDropDownListFor(model => model.Sex, "Select sex", new { @class = "form-control", type = "text"})

By this I have a dropdown with default option "Select sex", but validation accepts only options provided by enum ("Male" and "Female").

In MVC3 (without EnumDropDownListFor) I used in model:

    public Sex? Sex { get; set; }

    Sex = null;

    Sexes = Repository.GetAutoSelectList<Sex>("");

In view:

    @Html.DropDownListFor(model => model.Sex, Model.Sexes, new { @class = "form-control", type = "text" })

It's a bit hard to understand your question, but the default value for an enum is zero, which may or may not be a valid value for the enumeration.

If you want default(MyEnum) to be the first value, you'd need to define your enumeration like this:

public enum MyEnum
   First = 0,

Cannot display enum values on Kendo grid

You must set NotMapped attribute for custom property:

using System.ComponentModel.DataAnnotations.Schema;
public class VisitorViewModel
    public int VisitorID { get; set; }

    public Country Country { get; set; }

    public string CountryName
        get { return Country.GetDescription(); }

and GetDescription() is next extension method:

public static string GetDescription(this Enum e)
    var field = e.ToString();
    var attribute = e.GetType().GetField(field).GetCustomAttributes(typeof(DescriptionAttribute), false).FirstOrDefault();

    return attribute != null ? ((DescriptionAttribute)attribute).Description : field;