Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Incorrect warning for non-nullable field #76809

Open
Gavin-Williams opened this issue Jan 18, 2025 · 1 comment
Open

Incorrect warning for non-nullable field #76809

Gavin-Williams opened this issue Jan 18, 2025 · 1 comment
Labels
Area-Compilers untriaged Issues and PRs which have not yet been triaged by a lead

Comments

@Gavin-Williams
Copy link

Version Used:

.Net 8.0

Steps to Reproduce:

  1. Add a class reference to WNDPROC ( private static WNDPROC WindowProc; )
  2. Initialize it in the Window constructor ( WindowProc = ProcessWindowMessage; )\

Expected Behavior:

This should not report a warning ( CS8618 )

Actual Behavior:

I get the warning:

CS8618 Non-nullable field 'WindowProc' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the field as nullable.

@dotnet-issue-labeler dotnet-issue-labeler bot added Area-Compilers untriaged Issues and PRs which have not yet been triaged by a lead labels Jan 18, 2025
@bjornhellander
Copy link

bjornhellander commented Jan 18, 2025

Do you initialize it in an instance constructor or in the static constructor? If you meant an instance constructor, then this behavior seems correct, since the field is static. When the class is initialized, this field has not been assigned a non-null value. You could for example put the initialization in the field declaration (if possible), move the intialization to a static constructor (if possible) or remove the static modifier.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-Compilers untriaged Issues and PRs which have not yet been triaged by a lead
Projects
None yet
Development

No branches or pull requests

2 participants