0

I dont know why this so hard no..

I have to check is the user input in Enum.

Now i have two different methods, which tell (False or True) is input in the Enum. I do not want show those things to user, i just want program to print like:

"Your input "user input here" found in Car makers list with name "Audi"

using static System.Console;

namespace EnumAutomerkit
{
    enum Automerkit
    {
        Audi = 100,
        BMW = 101,
        Citroen = 102,
        Mercedes_Benz = 103,
        Skoda = 104,
        Toyota = 105,
        Volkswagen = 106,
        Volvo = 107
    }


    class Program
    {
        static void Main(string[] args)
        {
            WriteLine("AUTOMERKIT");

            foreach (int luku in Enum.GetValues(typeof(Automerkit)))
                WriteLine($"{luku} = {Enum.GetName(typeof(Automerkit), luku)}");

            while (true)
            {
                Console.Write("Valitse automerkki joko numerolla tai nimellä (tyhjä lopettaa): ");  //"Choose car maker with number or name (empty breaks): "
                string userinput = Console.ReadLine();

                int.TryParse(userinput, out int userinput2);//muuttaa annetun stringin int olioksi

                Console.WriteLine("{0}: {1}", userinput2, Enum.IsDefined(typeof(Automerkit), userinput2)); //this can check is the number user gave in enum
                Console.WriteLine("{0}: {1}", userinput, Enum.IsDefined(typeof(Automerkit), userinput)); //this can check is the text or chars user gave in enum


                if (userinput == "")
                {
                    break;
                }
            }
        }
    }
}
Osmankaapa
  • 124
  • 10

3 Answers3

3

An enum value is automatically converted to an enum name when converted to string. Therefore you can write

foreach (Automerkit enumValue in Enum.GetValues(typeof(Automerkit))) {
    WriteLine($"{(int)enumValue} = {enumValue}");
}

Note that the foreach statement automatically converts the element type (object returned by GetValues here) to the type of the iteration variable (Automerkit).

For more information, see The foreach statement section of the C# language specification. The parameter of the WriteLine uses String interpolation in C# (Microsoft Docs).

Olivier Jacot-Descombes
  • 104,806
  • 13
  • 138
  • 188
  • `GetValues` returns `System.Array` of the type passed into instead of object thereby not converting. – Soner from The Ottoman Empire Dec 08 '19 at 12:53
  • @snr, No. Try to use `foreach (var enumValue in Enum.GetValues(typeof(Automerkit))) { ...` with a `var` and you will see that `enumValue` will be inferred as `object`. (Note that the code still works with `var`, so converting the object to the enum type is not required but better reflects the intent of the programmer). Note, even if the array is an `Automerkit[]`, it is returned as `System.Array`. Therefore you have to cast either the whole array or its elements if you want an object explicitly typed as `Automerkit`. – Olivier Jacot-Descombes Dec 08 '19 at 13:10
  • Sir, at first, thank your effort and nice explanation but `Enum.GetValues(typeof(Automerkit))` returns type of `Automerkit[]`. `enumValue` is of type `object`. Do you mean the type is cast to `Automerkit` type implicitly? (object -> Automerkit). Would you mind glancing at https://i.stack.imgur.com/BWJ39.png? – Soner from The Ottoman Empire Dec 08 '19 at 14:44
  • See the definition of [Enum.GetValues(Type) Method](https://learn.microsoft.com/en-us/dotnet/api/system.enum.getvalues?view=netframework-4.8). It returns an [Array Class](https://learn.microsoft.com/en-us/dotnet/api/system.array?view=netframework-4.8) having an indexer [Array.IList.Item`[`Int32`]` Property](https://learn.microsoft.com/en-us/dotnet/api/system.array.system-collections-ilist-item?view=netframework-4.8) returning an `object`. The runtime type of the items is `Automerkit`, but the static type at compile time is `object`. To see the real object you must cast it. – Olivier Jacot-Descombes Dec 09 '19 at 13:40
2

You can use the Enum.GetNames function and then parse the name to get the int value.

foreach (var merkkiluettelo in Enum.GetNames(typeof(Automerkit)))
{
    Enum.TryParse(merkkiluettelo, out Automerkit a);
    Console.WriteLine($"{(int)a} = {merkkiluettelo}");
}
EylM
  • 5,967
  • 2
  • 16
  • 28
1

You can use the GetName method to get the name of the enum value associated with its underlying value (there are other methods too, see this post). You only need one foreach loop, and you can use string interpolation to print in the format value = name:

foreach (int rawValue in Enum.GetValues(typeof(Automerkit))) {
    WriteLine($"{rawValue} = {Enum.GetName(typeof(Automerkit), rawValue)}");
}
Sweeper
  • 213,210
  • 22
  • 193
  • 313