In this code snippet, we will see how to use enumerations in C#.
An enum is a data type. To create an enumeration you have to give it a name and define a list of constants that will be available when that enumeration is referenced.
In this code example, we will simulate a key being pressed. The getKey() method will return the key that was pressed. We could just return a string like so “left“. But making an enum with predefined values is much less error-prone than using a string. A misspelled string won’t get caught at compile time meanwhile if you misspell or chose a nonexisting enum an error will occur at compile time.
Let’s have a look at the code below.