C# Enum is a strongly typed constant and keyword for the new data type Enumeration. An effective technique to define a set of named integral constants that may be assigned to a variable is to use a Typesafe enumeration. Enums make the code easier to read and less likely to have errors. When you have a group of values that are both functionally significant and unchangeable, enums come in handy. Enums’ main benefit is that they make it simple to update values in the future. C# Enums are a more robust alternative to the simple String or Int constants used to express sets of related objects in earlier APIs.
Enums play an important role in C#. These are defined as the set of named integer constants. Also known as the value data type. Enums can’t Inherit or pass inheritance, because it contains their own value. C# enum keyword is used to declare the enumerated type.
Used of C# Enums
In the.NET framework, developers create enums to generate numeric constants. A number value must be assigned to each member of the enum. The following are the most important aspects of Enums:
In C#, keyword Enums are used to build enumerated data types.
- Enums are for programmers.
- Enums, as previously stated, are highly typed constants.
- It is not possible to indirectly assign an enum of one type to another enum type.
- Enumerations improve the readability, reusability, and comprehension of code.
- The enum values are set in stone.
- The int enum type is the default.
- Every enum type inherits from System by default.
- Enum
- Enums are value types that are built on the stack rather than the heap.
How to define C# Enums:
Enum is an abstract class in C# that includes static helper methods. The set of auxiliary methods is as follows:
Format: This function translates the supplied enum type value to the string format specified.
GetName: The name of the constant or the value of the provided enum type is returned by this function.
GetNames: It returns an array of string names for all of the enum’s constants.
GetValues: The values of all the constants in the specified enum are stored in an array.
Parse: The function converts a string representation of one or more enumerated constants’ name or numeric value to an equivalent enumerated object.
TryParse: Converts the string representation of one or more enumerated constants’ names or numeric values to an equivalent enumerated object.
The syntax for declaring an enum in C# is given below:
enum <enum_name> { enumeration list };
Where,
- The enum_name specifies the enumeration type name.
- The enumeration list is a comma-separated list of identifiers.
Each symbol in the enumeration list represents an integer value that is one greater than the symbol before it. The value of the first enumeration symbol is 0 by default. For instance,
enum Days { Sun, Mon, tue, Wed, thu, Fri, Sat };
The example below shows how to use an enum variable.
using System; namespace Enums{ public class Enumscode { enum FRUITS { Apple, Mango, Grapes, Orange}; public static void Main(string[] args) { int SummerFruit = (int)FRUITS.Mango; int WinterFruit = (int)FRUITS.Orange; Console.WriteLine("Mango: {0}",SummerFruit); Console.WriteLine("Orange: {0}",WinterFruit); } } }