RSS

Tag Archives: circular reference

Constant Enum: Circular definition

If you have declared an enum field as constant you may get following exception:

The evaluation of the constant value for … involves a circular Definition.

The code where this exception appears looks like:

private const Parity Parity = Parity.None;

Well, thats strange. There is no error. I declare a constant field with the same name as the name of the type. This is allowed, because I can also write following line of code, and this would compile:

private const String String = "HELLO";

The problem with enums is that the compiler interprets the Parity from Parity.None not as the name of the enum-type but as the name of the field. Hence, we have a circular reference because the constant is defined by the constant itself. To solve the compile error you have two options: either you add the fully qualified name to the third Parity value or you change the name of the field.

private const Parity Parity = System.IO.Ports.Parity.None;
//or
private const Parity ParityValue = Parity.None;

Note: If you use tools like ReSharper then after a code clean up the qualifier might be removed. If that is the case I would suggest you to use the second Option, to rename the the name of the field.

Advertisements
 
Leave a comment

Posted by on April 29, 2013 in C-Sharp

 

Tags: ,