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.
See help: http://stackoverflow.com/a/1167367/5524514
– user28595
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.
– Bruno Bermann
@Brunobermann not that I care about it, but giving credit wouldn’t be bad :)
– user28595
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.
– Bruno Bermann
@diegofm, thanks for the link, just had not understood how converted to int.
– Marco Souza