From ed1101449083ff72ae24bd661aa5b6aceaf6d317 Mon Sep 17 00:00:00 2001 From: Rageking8 <106309953+Rageking8@users.noreply.github.com> Date: Fri, 1 Mar 2024 00:03:03 +0800 Subject: [PATCH] Update CS0110 (#39749) --- docs/csharp/misc/cs0110.md | 61 +++++++++++++++++++------------------- 1 file changed, 31 insertions(+), 30 deletions(-) diff --git a/docs/csharp/misc/cs0110.md b/docs/csharp/misc/cs0110.md index 6df5ce5e213d4..f0af7bcde3dc6 100644 --- a/docs/csharp/misc/cs0110.md +++ b/docs/csharp/misc/cs0110.md @@ -1,42 +1,43 @@ --- description: "Compiler Error CS0110" title: "Compiler Error CS0110" -ms.date: 07/20/2015 -f1_keywords: +ms.date: 02/29/2024 +f1_keywords: - "CS0110" -helpviewer_keywords: +helpviewer_keywords: - "CS0110" ms.assetid: 0bfe7071-1194-4142-a1a1-6190ee92b1d4 --- # Compiler Error CS0110 -The evaluation of the constant value for 'const declaration' involves a circular definition - - The declaration of a [const](../language-reference/keywords/const.md) variable (`a`) cannot reference another const variable (`b`) that also references (`a`). - - The following sample generates CS0110: - -```csharp -// CS0110.cs -namespace MyNamespace -{ - public class A - { - public static void Main() - { - } - } - - public class B : A - { - public const int i = c + 1; // CS0110, c already references i - public const int c = i + 1; - // the following line would be OK - // public const int c = 10; - } -} -``` - +The evaluation of the constant value for 'const declaration' involves a circular definition + + The declaration of a [const](../language-reference/keywords/const.md) variable cannot reference another const variable such that a circular dependency is formed. This also applies to associated constant values of enum members. + +## Example + + The associated constant value of `MyClass.Color.Red` is explicitly set to `MyClass.Color.Blue`, but the value of `MyClass.Color.Blue` is dependent on the previous enum member (`MyClass.Color.Red`), hence both constant values cannot be determined. Similarly, the constant variable `MyClass.a` is defined in terms of `MyClass.b`, but that is also defined in terms of `MyClass.a`. + + The following sample generates CS0110: + +```csharp +// CS0110.cs +// compile with: /target:library +class MyClass +{ + enum Color + { + Red = Blue, // CS0110 + Blue, + } + + public const int a = b + 1; // CS0110 + public const int b = a + 1; +} +``` + +To resolve this error, break the circular reference by modifying or removing the definition. + ## See also - [Constants](../programming-guide/classes-and-structs/constants.md)