Null-coalescing out parameter doesnt give error

76 Views Asked by At

This question describes a error related to using Null-coalescing, Null-conditional and out parameter: Null-coalescing out parameter gives unexpected warning The answer details the reasons for this error.

But when I copy the example from the question to my project, I don't get an error and everything compiles. I encountered the fact that the project is compiled on my computer, but others have problems. Does it depend on the environment? What environment?

Project uses .net5 and С#8.0. I tried VS2022, Rider and console msbuild as compiler, all of them compile successfully.

2

There are 2 best solutions below

2
Guru Stron On BEST ANSWER

It seems that you have a later version of the compiler installed (compared to your colleagues machines). From the Select the .NET version to use doc:

The SDK uses the latest installed version

And C# 10 has a Improved Definite Assignment Analysis implemented which seems to fix the issue in the linked question (check out the examples).

Try adding global.json file to the project root to set the used SDK to compile the project. Something like:

{
  "sdk": {
    "version": "5.0.0",
    "rollForward": "latestMinor"
  }
}

And then try compiling again (my current guess is that it should result in error, another thing to check - .NET SDKs installed on your colleagues machines - use dotnet --list-sdks, in theory this check could just have been backported to .NET 5 SDK).

0
shingo On

The analyzer has been improved in C#10 for similar expressions, you can check these links for details.

[Proposal]: Improved Definite Assignment Analysis

Improved definite assignment

If you use an old compiler, the error is reproduciable. Here is an example on SharpLab.