Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can a variable typed as an enum take a value that is out of range of its elements?

Can anyone explain to me how an a member of this enum takes a value of 0?

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

enter image description here


enter image description here


enter image description here

like image 847
Homam Avatar asked Apr 18 '11 14:04

Homam


People also ask

Can enum be used as variable type?

An enum type is a special data type that enables for a variable to be a set of predefined constants. The variable must be equal to one of the values that have been predefined for it.

How are enumeration variables declared?

An enumeration type declaration gives the name of the (optional) enumeration tag. And, it defines the set of named integer identifiers (called the enumeration set, enumerator constants, enumerators, or members). A variable of the enumeration type stores one of the values of the enumeration set defined by that type.

What is the use of enum variable?

Enumeration or Enum in C is a special kind of data type defined by the user. It consists of constant integrals or integers that are given names by a user. The use of enum in C to name the integer values makes the entire program easy to learn, understand, and maintain by the same or even different programmer.

Can enum take null values?

Enum types cannot be nullable.


1 Answers

Whenever you use default(EnumLogicalOperator) or new EnumLogicalOperator() you'll get a zero value. In other words, the default value of an enum type member is always 0. All enums will have the value 0 until you set them to something else.

like image 162
Gabe Avatar answered Oct 11 '22 13:10

Gabe