dotnet / dotnet/roslyn

CS8618 suggests using `required` on static properties

Open
#78,152 1 comment 1 reaction 1 assignee Claimed by @333fred View on GitHub
Area-Compilers Concept-Diagnostic Clarity Feature - Required Members
Dominant language
C#
Stars
20.7k
Forks
4.3k
PR merge metrics
PR metrics pending

Description

**Version Used**: 4.14.0-3.25178.1 (`1a06295e`)

Both the warning message and code fix for CS8616 (non-nullable prop must be non-null after ctor) recommend using `required` on the property, even for static properties, where you can't use `required`.

**Steps to Reproduce**:

[SharpLab link](https://sharplab.io/#v2:EYLgxg9gTgpgtADwGwBYA+ABAzAAgwJhwGEcBvAWACgca9cMBGJPBgBhwBUYEAXMnAOYweAbhwBnYWIC+VWjgD0Cui2awAjgFcAlrAAmLdl178hoiVJyzK0oA===)

```cs
public class C {
public static string Text { get; set; }
// CS8616: Non-nullable property 'Text' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the property as nullable.
}
```

**Diagnostic Id**: CS8618 ("Non-nullable property must contain a non-null value when exiting constructor.")

**Expected Behavior**: The warning message does not contain a suggestion to make the property `required`, and/or the "Make property 'required'" code-fix does not show up.

**Actual Behavior**: Warning message is misleading and code-fix generates invalid code.

```cs
public class C {
public static required string Text { get; set; }
// CS0106: The modifier 'required' is not valid this item
}
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.