Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

95
Vistas
Trying to iterate trough enum using foreach and witch case

I made enum and tried to go through it with for-each loop and look for different cases using switch case and get values of dives used. ERROR I AM GETTING IS cannot implicitly convert type'console5.typeOfDevice' to 'string'

enum typeOfDevice
    {
        iphone=99,
        android=59,
        tablet=49
    }
var usedDevice = Enum.GetNames(typeof(typeOfDevice));
foreach (string used in usedDevice)
                {
                    switch (used)
                    {
                        case usedDevice.iphone: //gets error here
                            Console.Write($"enter how many time you used Iphone");
                            var input = Console.ReadLine();
                            break;
                        case usedDevice.android:
                            Console.Write($"enter how many time you used Android");
                            var input = Console.ReadLine();
                            break;
                        case usedDevice.tablet:
                            Console.Write($"enter how many time you used Tablet");
                            var input = Console.ReadLine();
                            break;
                        default:
                            break;
                    }
                }
over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

Enum.GetNames returns a string array. In your loop, you are trying to get usedDevice.iphone (etc) but usedDevice is a string array, therefore you'll get an error. However, if you change usedDevice to typeOfDevice, you'll get the comparison error you've edited into your question.

Namely, used is of type string, and it cannot implicitly be converted to typeOfDevice, since typeOfDevice is an enum, and not a string.

This question may be useful to you. In short, use GetValues over GetNames

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda