Search code examples
c#outnull-coalescing

Null-coalescing out parameter doesnt give error


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.


Solution

  • 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).