-
Notifications
You must be signed in to change notification settings - Fork 5.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
31 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) |