Get name, typecode, hashcode from Enum in C#

To calculate the total number of items defined in an enum in C#, you can use the Enum.GetNames method to get an array of all the enum names and then use the Length property of that array to get the total count:

int totalItems = Enum.GetNames(typeof(MyEnum)).Length;

To get the Enum string name from an int value in C#, you can use the Enum.GetName method:

int value = 1;
string name = Enum.GetName(typeof(MyEnum), value);

To get the typecode for an enum in C#, you can use the Type.GetTypeCode method:

TypeCode typeCode = Type.GetTypeCode(typeof(MyEnum));

To get the hashcode for an enum in C#, you can simply call the GetHashCode method on the enum value:

MyEnum value = MyEnum.SomeValue;
int hashCode = value.GetHashCode();
  1. C# get enum name: Getting the name of an enum in C#.

    DaysOfWeek day = DaysOfWeek.Monday;
    string enumName = Enum.GetName(typeof(DaysOfWeek), day);
    
  2. Retrieve type code of enum in C#: Retrieving the underlying type code of an enum in C#.

    TypeCode typeCode = Type.GetTypeCode(typeof(DaysOfWeek));
    
  3. Get hash code of enum in C#: Getting the hash code of an enum in C#.

    DaysOfWeek day = DaysOfWeek.Monday;
    int hashCode = day.GetHashCode();
    
  4. Enum.GetName method in C#: Using Enum.GetName to retrieve the name of an enum.

    DaysOfWeek day = DaysOfWeek.Monday;
    string enumName = Enum.GetName(typeof(DaysOfWeek), day);
    
  5. Getting enum name and value pairs in C#: Getting name and value pairs of an enum in C#.

    var enumPairs = Enum.GetValues(typeof(DaysOfWeek)).Cast<DaysOfWeek>()
                       .Select(e => new { Name = e.ToString(), Value = (int)e });
    
  6. C# enum type code example: Retrieving the underlying type code of an enum in C#.

    TypeCode typeCode = Type.GetTypeCode(typeof(DaysOfWeek));
    
  7. Calculating hash code for enum values in C#: Calculating hash codes for enum values in C#.

    int hashCode = DaysOfWeek.Monday.GetHashCode();
    
  8. Using Reflection to get enum information in C#: Using Reflection to retrieve information about an enum.

    Type enumType = typeof(DaysOfWeek);
    var enumValues = Enum.GetValues(enumType);
    var enumNames = Enum.GetNames(enumType);
    
  9. Accessing enum members and their properties in C#: Accessing properties of enum members in C#.

    public enum Colors
    {
        [Description("Red color")]
        Red,
    
        [Description("Green color")]
        Green,
    
        [Description("Blue color")]
        Blue
    }
    
    var redDescription = GetEnumDescription(Colors.Red);
    
    // ...
    
    private static string GetEnumDescription(Enum value)
    {
        FieldInfo field = value.GetType().GetField(value.ToString());
        DescriptionAttribute attribute = (DescriptionAttribute)Attribute.GetCustomAttribute(field, typeof(DescriptionAttribute));
        return attribute != null ? attribute.Description : value.ToString();
    }
    
  10. GetHashCode method for enum in C#: Getting the hash code of an enum using the GetHashCode method.

    DaysOfWeek day = DaysOfWeek.Monday;
    int hashCode = day.GetHashCode();