Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Default values for enums using the default keyword?

Tags:

c#

.net

enums

Anyone knows the default value for enums using the default keywords as in:

MyEnum myEnum = default(MyEnum);

Would it be the first item?

like image 880
Joan Venge Avatar asked Jan 22 '23 09:01

Joan Venge


1 Answers

It is the value produced by (myEnum)0. For example:

enum myEnum
{
  foo = 100
  bar = 0,
  quux = 1
}

Then default(myEnum) would be myEnum.bar or the first member of the enum with value 0 if there is more than one member with value 0.

The value is 0 if no member of the enum is assigned (explicitly or implicitly) the value 0.

like image 109
Richard Cook Avatar answered Jan 31 '23 04:01

Richard Cook