This is an elaboration on this question: c# Enum Function Parameters
I created a little sample application to introduce my question:
UPDATE: This is a known difficulty on the C# programming language. I added the used workaround in the code for people that find this in a search engine.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace FlexibleEnums
{
class Program
{
public enum Color
{
Blue,
Red,
Green
};
static void Main(string[] args)
{
CheckEnum<Color>();
Console.ReadKey();
}
private static void CheckEnum<T>()
{
foreach (T item in Enum.GetValues(typeof(T)))
{
Console.WriteLine(item);
// And here is the question:
// I would like to uncheck this line, but that does not compile!
//DoSomethingWithAnEnumValue(item);
// Solution:
// Not so nice, but it works.
// (In the real program I also check for null off cource!)
DoSomethingWithAnEnumValue(item as Enum);
}
}
private static void DoSomethingWithAnEnumValue(Enum e)
{
Console.WriteLine(e);
}
}
}
I think that I should be doing something like:
private static void CheckEnum<T>() where T : Enum
But that's also giving me compile errors.
Thanks for the help!