You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/csharp/misc/cs0236.md
+56-4Lines changed: 56 additions & 4 deletions
Original file line number
Diff line number
Diff line change
@@ -13,9 +13,27 @@ A field initializer cannot reference the non-static field, method, or property '
13
13
14
14
Instance fields cannot be used to initialize other instance fields outside a method.
15
15
16
+
## Why this error occurs
17
+
18
+
The compiler enforces this restriction because of how object initialization works in C#. When an object is created, field initializers are processed before any constructor code runs. During this phase:
19
+
20
+
1.**Field dependency restriction**: While field initializers are processed in lexical order within a single file, the rule prevents dependencies between fields to allow class authors to rearrange fields without introducing compiler errors. For partial classes, the order of field initializers across different source files isn't specified.
21
+
22
+
2.**Object is not fully constructed**: When field initializers run, the object instance is in an incomplete state. Allowing references between fields during this phase could lead to accessing uninitialized memory or unpredictable behavior.
23
+
24
+
3.**Compiler safety**: This restriction prevents potential runtime errors and ensures predictable object construction behavior.
25
+
26
+
The compiler detects this pattern during compilation and reports CS0236 to prevent these potential issues.
27
+
16
28
## To correct this error
17
29
18
-
If you are trying to initialize a variable outside a method, consider performing the initialization inside the class constructor. For more information, see [Methods](../programming-guide/classes-and-structs/methods.md).
30
+
1.**Move initialization to constructor**: Perform the initialization inside an instance constructor where all fields are guaranteed to be available.
31
+
32
+
2.**Use static fields**: If the referenced field doesn't need to be instance-specific, consider making it static.
33
+
34
+
3.**Use default values**: Initialize fields with literal values or expressions that don't reference other instance members.
35
+
36
+
4.**Lazy initialization**: Use properties with backing fields for complex initialization logic that depends on other instance members.
19
37
20
38
## Example
21
39
@@ -26,13 +44,47 @@ public class MyClass
26
44
{
27
45
publicinti=5;
28
46
29
-
// To fix the error, remove "= i", and uncomment the line in constructor.
47
+
// CS0236: Field initializer cannot reference instance field 'i'
48
+
// This restriction allows class authors to rearrange fields without compiler errors
30
49
publicintj=i; // CS0236
31
50
32
51
publicMyClass()
33
52
{
34
-
// Uncomment the following.
35
-
//j = i;
53
+
// This works because both fields are guaranteed to be initialized
54
+
// before constructor code runs
55
+
j=i;
56
+
}
57
+
}
58
+
```
59
+
60
+
## Additional examples
61
+
62
+
The following examples demonstrate different scenarios where CS0236 occurs:
63
+
64
+
```csharp
65
+
publicclassExamples
66
+
{
67
+
privatestringname="Default";
68
+
69
+
// CS0236: Cannot reference instance field in initializer
70
+
privatestringdisplayName=name.ToUpper();
71
+
72
+
// CS0236: Cannot reference instance method in initializer
73
+
privateintlength=GetNameLength();
74
+
75
+
// CS0236: Cannot reference instance property in initializer
0 commit comments