Dart Enumerations
Enumeration data type in Dart
In simple terms, Enumerations are referred to as named constant values. Constant values are declared as enumerations using the ‘enum’ keyword.
Implementation =
enum enum_name
{
enumeration
.
.
list
}
- enum_name refers to the name of the enumeration.
- The list is enclosed within the curly braces ‘ { } ’.
- The enumeration list mentioned here is the list of constant values separated by commas. Every value in the enum is associated with an integer value. The first value of the list corresponds to index ‘ 0 ’, and every value succeeds the other by +1.
- Note that the last member of the enumeration list doesn’t end with a comma or semi-colon.
For example,
enum prog_languages
{
Dart,
Flutter,
Java,
Python
}
Program
enum prog_language
{
Dart ,
Flutter ,
Java ,
JavaScript ,
Python ,
PHP ,
C ,
Go ,
SQL ,
MongoDb
}
void main( )
{
print( " Here is the list of Proramming Language Enumeration " ) ;
print( prog_language.values ) ;
prog_language.values.forEach( ( i ) => print( ' Value : $i, Index : ${ i.index } ' ) ) ;
print( prog_language.values[ 5 ] ) ;
}
Output
Here is the list of Proramming Language Enumeration
[prog_language.Dart, prog_language.Flutter, prog_language.Java, prog_language.JavaScript, prog_language.Python, prog_language.PHP, prog_language.C, prog_language.Go, prog_language.SQL, prog_language.MongoDb]
Value : prog_language.Dart, Index : 0
Value : prog_language.Flutter, Index : 1
Value : prog_language.Java, Index : 2
Value : prog_language.JavaScript, Index : 3
Value : prog_language.Python, Index : 4
Value : prog_language.PHP, Index : 5
Value : prog_language.C, Index : 6
Value : prog_language.Go, Index : 7
Value : prog_language.SQL, Index : 8
Value : prog_language.MongoDb, Index : 9
prog_language.PHP
Program
enum college_semester
{
first ,
second ,
third ,
fourth ,
fifth ,
sixth ,
seventh ,
eighth
}
void main( )
{
print( college_semester.values ) ;
print(" \n " ) ;
college_semester.values.forEach( ( v ) => print( ' value : $v, index : ${ v.index } ' ) ) ;
print( " \n " ) ;
print( ' You are in : ${ college_semester.seventh } ' ) ;
print( " \n " );
print( ' College semester 3rd corresponds to index : ${ college_semester.seventh.index } ' ) ;
}
Output :
[college_semester.first, college_semester.second, college_semester.third, college_semester.fourth, college_semester.fifth, college_semester.sixth, college_semester.seventh, college_semester.eigth]
n
value : college_semester.first, index : 0
value : college_semester.second, index : 1
value : college_semester.third, index : 2
value : college_semester.fourth, index : 3
value : college_semester.fifth, index : 4
value : college_semester.sixth, index : 5
value : college_semester.seventh, index : 6
value : college_semester.eigth, index : 7
You are in : college_semester.seventh
College semester 3rd corresponds to index : 6
Note : If one tries to print the value against an index that doesn’t exist in the enumeration list, the compiler throws an error.
For example,
enum prog_language
{
Dart ,
Flutter ,
Java ,
JavaScript ,
Python ,
PHP ,
C ,
Go ,
SQL ,
MongoDb
}
void main( )
{
print( prog_language.values[ 5 ] ) ;
print( prog_language.values[ 12 ] ) ;
}
Output :
Drawbacks of enumerations:
- The values in an enumeration cannot be instantiated separately.
- Enumerations cannot be subclassed.