Reputation: 231
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
Upvotes: 1
Views: 60
Reputation: 231
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();
Upvotes: 0
Reputation: 726539
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
.
Upvotes: 1