Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

89
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda