Sonar analysis does not handle correctly multi targeting

  • Operating system: Windows
  • Visual Studio version: 2022 (17.9.5)
  • SonarLint plugin version: 7.8.0
  • Programming language you’re coding in: C#
  • Is connected mode used:
    • Connected to SonarQube (and which version):10.4.1

Assuming a project multi targeting to netstandard2.1 and net472:

<Project Sdk="Microsoft.NET.Sdk">



Analyzing the following example I run into S3240:

namespace S3240
    public class Class1
        private object a;

        public void Test()
            if (a == null) // S3240
                a = new object();

The identification itself is ok, but the quick-fix changes the code to

a ??= new object();

This is correct for netstandard2.1 but not compatible with net472 (which is using C# 7.3).

When multi-targeting is enable the quick-fix should use the most compatible version which is

a = a ?? new object();

BTW: I guess this problem also applies to many more rules.

Hello @lg2de,

Thank you for reaching out!

The suggested code fix depends on which target framework your IDE uses.
In Visual Studio, you can choose which target framework to use by selecting it in the left-most dropdown on top of your file:


In your case, you are probably using netstandard2.1; thus, the suggested code fix is for netstandard2.1.

This happens because, when doing the analysis, we get the information only about the currently used target framework, we do not know about any other targeted framework.

Similarly, the code fix for IDE0074 will suggest you to use a null-coalescing assignment even though you are also targeting net472.

To prevent the wrong code fix from being suggested, make sure to always set the current target framework to net472.