Reputation: 47
I want to store a list of enum's in a C# list. I don't want to store an enum value but rather an enum. My goal is to ask a user to name some enum's that they want and store them in the list so I can traverse the list later.
So for example, I'll ask what categories of food are they interested in and they'll specify meats, vegetables, dairy, fruits, etc. Depending on what they want, I will tell them what's available.
If the user says the want to know about the meats and fruits categories, I will print out all enumerated meats and fruits like so...
beef, pork, chicken, apple, orange, banana
My enums will be defined like so...
enum meats {beef, pork, chicken};
enum vegetables {corn, lettuce, tomato, carrots, broccoli};
enum dairy {cheese, milk, yougurt};
enum fruits {apple, orange, banana};
Since the user picked meats and vegetables, I would like to add it to a List<> then traverse that list and print out all items in each enum. Semantically it would look like...
List<enum> toPrint = new List<enum>(); //list of enum "categories"
ShopperList(meats);
ShopperList(fruits);
void ShopperList(enum category){
toPrint.Add(category);
}
void PrintCategories(toPrint){
foreach (enum category in toPrint){
foreach(string name in Enum.GetNames(typeof(category)){
Console.WriteLine(name + ", ");
}
}
}
I get an error saying "Type expected" from the compiler on the line where I define the List variable. Is there a way to make this work?
Upvotes: 0
Views: 1110
Reputation:
An enum value is by default an int.
It seems you want to use polymorphism.
It is impossible with enums.
You may use classes.
using System;
using System.Reflection;
using System.Collections.Generic;
namespace ConsoleApp1
{
public abstract class Food { }
public abstract class Meat : Food { }
public class Beef : Meat { }
public class Chicken : Meat { }
public abstract class Vegetable : Food { }
public class Lettuce : Vegetable { }
public class Tomato : Vegetable { }
public class Carrots : Vegetable { }
class Program
{
static List<Type> toPrint = new List<Type>();
static string GetCategories(List<Type> list)
{
var result = new List<string>();
foreach ( var item in list )
foreach ( var type in Assembly.GetExecutingAssembly().GetTypes() )
if ( type.IsSubclassOf(item) )
result.Add(type.Name);
return String.Join(", ", result.ToArray());
}
static void Main()
{
toPrint.Add(typeof(Meat));
toPrint.Add(typeof(Vegetable));
Console.WriteLine(GetCategories(toPrint));
Console.ReadKey();
}
}
}
It prints:
Beef, Chicken, Lettuce, Tomato, Carrots
Upvotes: 0
Reputation: 37113
You´d need a List<Type>
, that stores the actual types, not the values
List<Type> toPrint = new List<Type>(); //list of enum "categories"
ShopperList(typeof(meats));
ShopperList(typeof(fruits));
void ShopperList(Type category){
toPrint.Add(category);
}
void PrintCategories(List<Type> toPrint){
foreach (Type category in toPrint){
foreach(string name in Enum.GetNames(category)){
Console.WriteLine(name + ", ");
}
}
}
As Type
can be anything, including e.g. string
, MyClass
or just object
, you should further filter on Type.IsEnum
within your loop:
foreach (Type category in toPrint.Where(x => x.IsEnum))
{
...
}
Upvotes: 4