According to this answer we can have generics constrained to System.Enum since C# 7.3.
However, while one can check any flag of an unkown Enum with
HasFlag(Enum flag)
it is not possible so set a flag of an unkown Enum, since there is no SetFlag() and the |-operator is not defined for System.Enum.
Basicly what I want to do is the following:
public static T Combine<T>(this IEnumerable<T> values) where T : System.Enum
{
var value = default(T);
foreach (var v in values)
{
value |= v;
}
return value;
}
The C# compiler tells me
The |=-operator cannot be applied to operands of type 'T' and 'T'.
Is it possible to create such an extension method anyways?