I am looking to check the value of an enum set in a generic class. When I try to write a basic if statement if (item.Value == AlphaType.A1)
, I got the following error:
Operator '==' cannot be applied to operands of type 'T' and 'Program.AlphaType'
Here is the code:
public enum AlphaType
{
A1,
A2
}
public enum BetaType
{
B1,
B2
}
public class Item<T>
{
public T Value { get; set; }
public string Foo { get; set;}
}
public static void Main()
{
var item1 = new Item<AlphaType> { Value = AlphaType.A1, Foo = "example 1" };
var item2 = new Item<BetaType> { Value = BetaType.B1, Foo = "example 2" };
PrintAlphaFoo(item1);
PrintAlphaFoo(item2);
}
public static void PrintAlphaFoo<T>(Item<T> item)
{
if (item.Value == AlphaType.A1)
{
Console.WriteLine(item.Foo);
}
}
Here the code should output example 1 but not example 2.