Default values ​​for listings using the default keyword?

Anyone knows the default value for listings using the default keywords, as in:

MyEnum myEnum = default(MyEnum);

Will this be the first item?

+3
source share
4 answers

This value is expressed (myEnum)0. For instance:

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

Then it default(myEnum)will be myEnum.bareither the first member of the enumeration with the value 0, if there is more than one element with the value 0.

Value 0if the enumeration member is not assigned (explicitly or implicitly) the value 0.

+4
source

, , .

, default(MyEnum) , :

public enum MyEnum
{
   First = 0,
   Second,
   Third
}
+2

default(T) (T)0 , enum. , enum 0.

enum E1 {
  V1 = 1;
  V2 = 2;
}

...

E1 local = default(E1);
switch (local) {
  case E1.V1:
  case E1.V2:
    // Doesn't get hit
    break;
  default:
    // Oops
    break;
}
+2

vs2k10.....

:

 enum myEnum
 {
  a = 1,
  b = 2,
  c = 100
 };



 myEnum  z = default(en);

: z == 0, ( )

visual studio 2k10

0

Source: https://habr.com/ru/post/1766101/


All Articles