public enum MyEnum{Value1, Value2}
class MyClass
{
private MyEnum _field;
public MyEnum Field // added for convenience
{
get { return _field;
Is there any better way for this problem?
If you need to use Interlocked.Exchange
then this is the best way, in fact I think it is the only way to Exchange an enum.
The reason you get the compiler error is that the compiler thinks you want to use Exchange
, but T needs to be a reference type for this to work, since you are not using a reference type it fails. So, the best work around is to cast to an int
as you have done, and thus force the compiler to use the non-generic Exchange(int, int)
.