English 中文(简体)
Dart Programming - Enumeration
  • 时间:2024-10-18

Dart Programming - Enumeration


Previous Page Next Page  

An enumeration is used for defining named constant values. An enumerated type is declared using the enum keyword.

Syntax

enum enum_name {  
   enumeration pst 
}

Where,

    The enum_name specifies the enumeration type name

    The enumeration pst is a comma-separated pst of identifiers

Each of the symbols in the enumeration pst stands for an integer value, one greater than the symbol that precedes it. By default, the value of the first enumeration symbol is 0.

For example

enum Status { 
   none, 
   running, 
   stopped, 
   paused 
}

Example

enum Status { 
   none, 
   running, 
   stopped, 
   paused 
}  
void main() { 
   print(Status.values); 
   Status.values.forEach((v) => print( value: $v, index: ${v.index} ));
   print( running: ${Status.running}, ${Status.running.index} ); 
   print( running index: ${Status.values[1]} ); 
}

It will produce the following output

[Status.none, Status.running, Status.stopped, Status.paused] 
value: Status.none, index: 0 
value: Status.running, index: 1 
value: Status.stopped, index: 2 
value: Status.paused, index: 3 
running: Status.running, 1 
running index: Status.running 
Advertisements