Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions docs/csharp/misc/cs0236.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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:
Expand Down
Loading