As answer in the SO , you 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
}
See working on dotNetFiddle .
The solution will obviously vary as needed. Then another possibility would be:
var lista = Enum.GetValues(typeof(TiposHospedagem)).Cast<int>().ToList();
See running on dotNetFiddle .
It may not be very performative as it uses reflection, but should meet most needs well.
This code takes all the existing values in the enumeration, makes a cast on each member for 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.