What is the best practice of enum in C#? Archives - Tech Insights Unveiling Tomorrow's Tech Today, Where Innovation Meets Insight Wed, 20 Sep 2023 06:16:26 +0000 en-US hourly 1 https://wordpress.org/?v=6.6.2 https://i0.wp.com/reactconf.org/wp-content/uploads/2023/11/cropped-reactconf.png?fit=32%2C32&ssl=1 What is the best practice of enum in C#? Archives - Tech Insights 32 32 230003556 C# Enum | How to use Enum in C# https://reactconf.org/how-to-use-enum-in-csharp/ https://reactconf.org/how-to-use-enum-in-csharp/#respond Wed, 20 Sep 2023 06:16:26 +0000 https://labpys.com/?p=1715 In C#, Enum is a value data type defined by a set of named constants. Enum is the short form of Enumeration. In this article, we will learn How to …

The post C# Enum | How to use Enum in C# appeared first on Tech Insights.

]]>
In C#, Enum is a value data type defined by a set of named constants. Enum is the short form of Enumeration. In this article, we will learn How to use Enum in C#. 

 How to use Enum in C#

Using Enum in C#

It consumes less memory and space because they are value types, and Enum are strong types named constants.

A few key points about Enum:

  • Values are fixed.
  • strongly typed constants
  • It cannot be allocated automatically from one form to another type of enum.

How to Create an Enum

To create Enum use the keyword enum specify the names of enum members and separate the enum items with a comma. As an example.

        public enum Gender
        {
            Male,
            Female,
            Others = 101
        }

In the above example, an enum for the Gender is created. In enum values are started from 0 by default if we do not assign them, the values are incremented by 1 for the next enum member.

by default enum element is int. We can set different types by adding a colon as an example.

        public enum Gender : byte
        {
            Male,
            Female,
            Others = 101
        }

The different types which can be set are short,ushort, uint, ulong, sbyte , byte, and long.

How to Get the Value of an Enum

To get the value of enum we can simply typecast it to its type. If the enum type is int we have to typecast it to int. We can also get the string value using the ToString() method as below.

 Console.WriteLine((int)Gender.Male);
 string gender = Gender.Male.ToString();
 Console.WriteLine(gender);

Output:

Gender is in number - 0
Gender is in string  -Male

Enum in a Switch Statement

Enum is often used in switch statements to check for corresponding values.

            Gender myGen = Gender.Male;
            switch (myGen)
            {
                case Gender.Male:
                    Console.WriteLine($"Gender is {myGen}");
                    break;
                case Gender.Female:
                    Console.WriteLine($"Gender is {myGen}");
                    break;
                case Gender.Others:
                    Console.WriteLine($"Gender is {myGen}");
                    break;

            }

Output:

 
Gender is Male

Parse the string value into an Enum

Enum.Parse – this method directly parses the string represented by the name or numeric value of an enum member into an enum Type object. If the string representation of the name is not found, then it will give the exception.

  Console.WriteLine(Enum.Parse(typeof(Gender), "Female"));

Output:

Parse Method -Female

Enum.TryParse – this method gives the result in bool value. If the string representation of the name or numeric value of an enum member into an enum Type object is parsed, then the result will be true, and the parsed value will be in the out variable. If the string value is not parsed, then the result will be false.

  Enum.TryParse("1", out Gender values);
  Console.WriteLine(values);
TryParse Method - Female

How to check value is defined in the Enum string or int

 Console.WriteLine(Enum.IsDefined(typeof(Gender), 0));

 Console.WriteLine(Enum.IsDefined(typeof(Gender), "Female"));
check Enum is int -  True
check Enum is string - True

How to get All Enum Value through Loop

Iterate enum values through the loop, as in the below example.

            foreach (var item in Enum.GetNames(typeof(Gender)))
            {
            Console.WriteLine($"{item}" + (int)Enum.Parse(typeof(Gender), item));
            }
Looping  -  Male0
Looping  -  Female1
Looping  -  Others101

Above example Enum.GetNames(Types enum) method is used, which retrieves an array of the names from the specified enum.

What is Enum Flags

Flags enum type to represent a combination of choices defines enum members for those choices such that an individual choice is a bit field.

            [Flags]
        enum PerGranted
        {
            Read = 1,
            Write = 2,
            None = 3
        }

         PerGranted myGrante = PerGranted.Read | PerGranted.Write;
            if (myGrante == PerGranted.Write)
            {
                Console.WriteLine($"Permission Granted {myGrante}");
            }
Permission Granted

More article – C# forEach loop | When to Use, How it Works

The post C# Enum | How to use Enum in C# appeared first on Tech Insights.

]]>
https://reactconf.org/how-to-use-enum-in-csharp/feed/ 0 1715