How does a variable typed as enum take on a value that goes beyond its elements?

Can someone explain to me how the member of this enum takes on meaning 0?

public enum EnumLogicalOperator
{
    And = 1,
    Or = 2
}

enter image description here


enter image description here


enter image description here

+5
source share
8 answers

Whenever you use default(EnumLogicalOperator)or new EnumLogicalOperator(), you will get a null value. In other words, the default value for an element of type enum is always 0. All enumerations will be set to 0 until you set them to something else.

+5
source

, . , 0, :

  • .
  • , # 0 .
+4

: http://msdn.microsoft.com/en-us/library/sbbt4032%28v=vs.80%29.aspx

E , (E) 0

, 0. , , LogicalOperatorWithPreviousRule .

+4

0. int, , 0.

:

E , (E) 0.


- :

EnumLogicalOperator x = EnumLogicalOperator.Or;

, 0 , .


, , . :

meetingDay. , : meetingDay = (Days) 42. , , enum , . , .

# ( , 1.10 ):

, , . , .

, , . , . :

. , enum . , .

+3

# ( 1.10, ):

, . , .

, 4.1.2 , 0.

, : -)

+3

An enumeration can have any value based on its base type (Int32 in your case).

EnumLogicalOperator op = (EnumLogicalOperator) 0;
EnumLogicalOperator op = (EnumLogicalOperator) 100;
EnumLogicalOperator op = (EnumLogicalOperator) 900000;
EnumLogicalOperator op = (EnumLogicalOperator) -1;

private EnumLogicalOperator _op; // As a class member, will default to 0

Be sure to use Enum.IsDefined when checking your input conditions and error handling. It is also usually a good idea to include 0 as the value for one of your enum values.

+3
source

I suggest you include the "Undefined" member with a value of 0, or make your "And" or "Or" a value of 0.

0
source

All Articles