dotnet / docs

This repository contains .NET Documentation.
https://learn.microsoft.com/dotnet
Creative Commons Attribution 4.0 International
4.26k stars 5.89k forks source link

Warning CS8632 The annotation for nullable reference types should only be used in code within a '#nullable' annotations context #26736

Closed JamalAPI closed 2 years ago

JamalAPI commented 2 years ago

Getting a warning on compile on Joke[]? jokes :

Warning CS8632 The annotation for nullable reference types should only be used in code within a '#nullable' annotations context. WorkerService1 JokeService.cs 27

// The API returns an array with a single entry. Joke[]? jokes = await _httpClient.GetFromJsonAsync<Joke[]>( JokeApiUrl, _options);

also on Joke? joke = jokes?[0];

Removing the ? corrected the issue per the VS fix it recommendation.


Document Details

Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.

IEvangelist commented 2 years ago

The linked source code that's part of this example does in fact have a nullable context:

https://github.com/dotnet/samples/blob/02542327173a035562ec7cc3580244d576ed38d9/core/workers/windows-service/App.WindowsService.csproj#L6

But I see that the steps do not include it, we should add that to make it more clear. I'll do that, thank you.