diff --git a/docs/csharp/misc/cs0236.md b/docs/csharp/misc/cs0236.md index 4a90b6fb503f9..44444f4faf89c 100644 --- a/docs/csharp/misc/cs0236.md +++ b/docs/csharp/misc/cs0236.md @@ -35,6 +35,8 @@ The compiler detects this pattern during compilation and reports CS0236 to preve 4. **Lazy initialization**: Use properties with backing fields for complex initialization logic that depends on other instance members. +5. **Primary constructor**: Use [primary constructors](../programming-guide/classes-and-structs/instance-constructors.md#primary-constructors) to initialize the public field with the value of instance field or value given as constructor argument. + ## Example The following sample generates CS0236, and shows how to fix it: @@ -57,6 +59,22 @@ public class MyClass } ``` +CS0236 can also be fixed using the *primary constructor* as shown in the following example: + +```csharp +public class MyClass +{ + public int i = 5; + public int j => i; +} + +// or +public class MyClass(int i) +{ + public int j => i; +} +``` + ## Additional examples The following examples demonstrate different scenarios where CS0236 occurs: