arad
arad

Reputation: 33

Enum and functions

How to create a function which gets an enum and print it values and names at the same time? If this is my Enum:


    public enum DaysOfWeek
    {
        monday,
        tuesday,
        wednesday
    }

I tried :

but it's not working

    public void ShowEnum(Enum EnumList)
    {
        foreach (var route in Enum.GetValues(typeof(EnumList)))
        {
            Console.WriteLine($"{(int)route} - {route}");
        }
    }

I wish to print :

0 - monday 1 - tuesday 2 - wednesday


Upvotes: 0

Views: 142

Answers (4)

Filburt
Filburt

Reputation: 18062

Other answers already got pretty close - I figured Enum.Format is the way to go:

public static void ShowEnum<T>() where T : Enum
{
    foreach (var t in (T[])Enum.GetValues(typeof(T)))
    {
        Console.WriteLine($"{Enum.Format(typeof(T), t, "d")} - {t}");
    }
}

Upvotes: 1

Tor
Tor

Reputation: 631

Something like this should work (I cant try it in VS right now)

public void ShowEnum<TEnum>()
    where TEnum : Enum
{
    foreach (var enumValue in Enum.GetValues(typeof(TEnum)))
    {
        Console.WriteLine($"{(int)enumValue} - {enumValue}");
    }
}

As you can see, there is a generic parameter TEnum in the method with an Enum constraint, so you can pass any enum to the method.

usage:

ShowEnum<DaysOfWeek>();

If you don't have Enum generic constraint, you are using older C# version (this constraint was introduced in C# 7.3)

If this is the situation, you need some extra check, like this:

public void ShowEnum<TEnum>()
    where TEnum : struct, IConvertible
{
    if (!typeof(TEnum).IsEnum) 
    {
        throw new ArgumentException("TEnum is not an enum");
    }

    foreach (var enumValue in Enum.GetValues(typeof(TEnum)))
    {
        Console.WriteLine($"{(int)enumValue} - {enumValue}");
    }
}

Upvotes: 0

Serge
Serge

Reputation: 43880

Try these ( all code was tested in Visual Studio and works properly):

var i=1;
foreach (DaysOfWeek dayOfWeek in (DaysOfWeek[])Enum.GetValues(typeof(DaysOfWeek)))
    {
        Console.WriteLine( i.ToString() +" - " dayOfWeek.ToString());
       i+=1;
    }

or maybe (it will start from 0)

foreach (DaysOfWeek dayOfWeek in (DaysOfWeek[])Enum.GetValues(typeof(DaysOfWeek)))
    {
        Console.WriteLine( ((int) dayOfWeek).ToString() + " - "+  dayOfWeek.ToString());
    }

Or

public static void EnumPrint<T>() where T : Enum
{
    var i = 1;
    foreach (var t in (T[])Enum.GetValues(typeof(T)))
    {
        Console.WriteLine((i).ToString() + " - " + t.ToString());
        i += 1;
    }
}

or in one line:

var enumValues = Enum.GetValues(typeof(DaysOfWeek));

Upvotes: 1

Yennefer
Yennefer

Reputation: 6234

You can have the name of enumeration value in this way

Enum.GetName<DaysOfWeek>(value)

Therefore

        public void ShowEnum(Enum EnumList)
        {
            foreach (var route in Enum.GetValues(typeof(EnumList)))
            {
                Console.WriteLine($"{(int)route} - {Enum. GetName<DayOfWeek>(route) }");
            }
        }

Upvotes: 0

Related Questions