1

Can I in MVC C# add an element to enum with name (default)?

public enum Errs
{
    default, primary, success, info, warning, danger
}

I received this error: Identifier expected; 'default' is a keyword

1
  • 1
    You can add an @ before default. But I advise you to use Pascal case. in your case: Default, Primary Commented May 13, 2018 at 11:34

2 Answers 2

1

You can do that using the @ syntax for using reserved words as identifiers:

public enum Errs
{
    @default, primary, success, info, warning, danger
}

However, I would recommend using some other name that is not a C# keyword, and add [DisplayName] attribute:

public enum Errs
{
    [DisplayName(Name="default")]
    Default,
    [DisplayName(Name="primary")]
    Primary,
    [DisplayName(Name="success")]
    Success,
    [DisplayName(Name="info")]
    Info,
    [DisplayName(Name="warning")]
    Warning,
    [DisplayName(Name="danger")]
    Danger
}

Check this Q&A if you need to obtain string names from DisplayName.

Sign up to request clarification or add additional context in comments.

Comments

0

Thanks to AliJP

I Modified my code as:

public enum Errs
{
    Default = 0,
    Primary = 1,
    Success = 2,
    Info = 3,
    Warning = 4,
    Danger = 5,
}

var messageKind = Errs.Danger.ToString().ToLower();

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.