How do you convert an Enum guy into a list?

Asked

Viewed 1,642 times

5

Is there any way to convert a Enum in List?

public enum TiposHospedagem
{
    Casa = 1,
    Hotel = 2,
    Pousada = 3,
    CasaCampo = 4
}

I’m trying to run the enum and add to the list, but the foreach does not agree to do this.

protected void Page_Load(object sender, EventArgs e)
{
    var lista = new List<Int32>();

    foreach (var th in TiposHospedagem)
    {
        lista.Add(Convert.ToInt32(th));
    }
}
  • 2

    See help: http://stackoverflow.com/a/1167367/5524514

  • diegofm, I know your intention is to help, but I posted a free translation of the answer in the OS in English, because many do not understand the language and for this reason use the OS in Portuguese.

  • @Brunobermann not that I care about it, but giving credit wouldn’t be bad :)

  • 1

    I put a link to the original post, would it be better if I put some more information? My intention is to collaborate as much as possible.

  • @diegofm, thanks for the link, just had not understood how converted to int.

2 answers

8


In accordance with response in the OS, can do this:

using System;
using System.Linq;
using static System.Console;
using System.Collections.Generic;
                    
public class Program {
    public static void Main() {
        var lista = new List<int>();
        foreach (int item in Enum.GetValues(typeof(TiposHospedagem))) lista.Add(item);
        foreach (var item in lista) WriteLine(item);
    }
}

public enum TiposHospedagem {
    Casa = 1,
    Hotel = 2,
    Pousada = 3,
    CasaCampo = 4
}

Behold working in the ideone. And in the .NET Fiddle. Also put on the Github for future reference.

The solution obviously may vary depending on the need. Then another possibility would be:

var lista = Enum.GetValues(typeof(TiposHospedagem)).Cast<int>().ToList();

Behold working in the ideone. And in the .NET Fiddle. Also put on the Github for future reference.

It may not be very performatic since it uses reflection, but it should suit most needs.

This code takes all values existing in the enumeration, makes a cast in each of the members to an integer, since it is the type you want (if you wanted another could be a problem) and converts to the list. All this using LINQ, so you can do it in one line.

  • Thanks, it worked out here....

  • 2

    Why not just use foreach (int item in Enum.GetValues(typeof(TiposHospedagem)))?

  • @because the result of each item returned by GetValues() will be a object and if you want a int. It is known that the result will be a int, but need to say it pro compiler. Bad API.

  • No, he compiles.

  • @ramaral ah, there’s a cast implicit, could be done as well. I modified, it gets better. Credits for you in this part.

  • 1

    But you’re right: you need to say this pro compiler, using int item instead of var item

Show 1 more comment

2

You can use the following syntax:

Enum.GetValues(typeof(MeuEnumerador)).Cast<MeuEnumerador>();

And this will return a specialized Ienumerable interface to the type Meuenumerador.

To convert the data through this interface to a list you must use its ". Tolist()" method

Observing: You need to include the System.Linq namespace in your code to use this code.

Credits: Answer based on the SO-En question located in this link

Browser other questions tagged

You are not signed in. Login or sign up in order to post.