3

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.

  • Do you have nullable context on or off? – Charlieface Jun 20 '23 at 09:34
  • [If you're using .NET 5 you _won't_ be using C# 8.0 - .NET 5 is tied-to C# 9.0](https://learn.microsoft.com/en-us/dotnet/core/whats-new/dotnet-5). MSBuiild will bump your language version to match the current .NET version target - and IIRC it will just ignore any attempt to override it via your `.csproj`. – Dai Jun 20 '23 at 09:37
  • @Dai: MSBuild respects what you explicitly put in `LangVersion` yourself, regardless of your .NET version and whether or not it will work. You can use C# 11 with .NET 4.8 if you so please, and it will work as long as no features are used that require runtime and/or library support. – Jeroen Mostert Jun 20 '23 at 09:43
  • My .csproj file does not have element, so it is at default setting. – Eugene Pavlyuk Jun 20 '23 at 09:43
  • I tried to create new console solution and code from question still works for me, so lang version doesnt matter. – Eugene Pavlyuk Jun 20 '23 at 09:45
  • @JeroenMostert "whether or not it will work" is the operative phrase though: the entire build system (both MSBuild and `csc.exe` itself) conspire against using C# 8.0 or higher when targeting .NET Framework 4.x alone _unless_ you're using multi-targeting, for example - otherwise you get an unsupported lang-version+target error message and the build fails. – Dai Jun 20 '23 at 09:50
  • What .NET SDKs do you have installed? Have you tried "fix" one used for the project by using [global.json](https://learn.microsoft.com/en-us/dotnet/core/tools/global-json)? – Guru Stron Jun 20 '23 at 09:56
  • @Dai: I can't replicate it. Maybe if you're using old-style project files, or starting things from old MSBuild versions rather than the SDK? New-style project files (which are required to be able to do multi-targeting, but they can single-target just fine) allow any combination of `` and `` (and I do mean any -- C# 1 and .NET 7 combine, if you insist, though few non-trivial programs could be compiled that way). If you're using .NET 5 as the OP is, you are *certainly* using `dotnet` and a new-style project file, so there's definitely no issue. – Jeroen Mostert Jun 20 '23 at 09:56

2 Answers2

1

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.

shingo
  • 18,436
  • 5
  • 23
  • 42
1

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

Guru Stron
  • 102,774
  • 10
  • 95
  • 132
  • It means I get "Improved Definite Assignment Analysis" from C# 10, but not other new features like "not" pattern? Which doesn't work for me. – Eugene Pavlyuk Jun 20 '23 at 10:10
  • @EugenePavlyuk basically yes. It will require a more deep knowledge of the compiler but analysis can be implemented as language-version agnostic while patterns require specific language version set. You actually can try setting a higher language version than a supported one by used .NET version and some of newer language stuff will work (if newer compiler is present). – Guru Stron Jun 20 '23 at 10:13