I have a type Camp that returns a string list and the bellow code validates using enum, I need to make this returns a list of strings and I can't use enum in this case.
How do I do it?
public string getChampionships()
{
string[] enumNames = Enum.GetNames(typeof(Camp));
Dictionary<string, int> dic = new Dictionary<string, int>();
Array.ForEach(enumNames, val => dic[val] = (int)Enum.Parse(typeof(Camp), val));
return JsonConvert.SerializeObject(dic);
}
Since Enum.GetNames(typeof(Camp));
returns an array of strings, just go ToList
.
public IList<string> getChampionships()
{
return Enum.GetNames(typeof(Camp)).ToList();
}
if your are trying to create some type of map IDictionary<string,int>
then your method would look something like this.
public IDictionary<string,int> getChampionships()
{
return Enum.GetNames(typeof(Camp))
.ToDictionary(item => item, item => (int)Enum.Parse(typeof(Camp), item));
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.