0

Environment: vs 2022 v 17.4.0, with NET7 sdk installed

I have a multi target project net7.0;net472:

<PropertyGroup>
        <OutputType>Exe</OutputType>
        <TargetFrameworks>net7.0;net472</TargetFrameworks>      
        <LangVersion>11.0</LangVersion>
        <!--others-->
    </PropertyGroup>

I tried to use the new feature required in c# 11 as given below:

public class Person
    {
        public required int Id { get; set; }
       
    }

I get a compilation error in net472:

Error CS0656 Missing compiler required member 'System.Runtime.CompilerServices.CompilerFeatureRequiredAttribute..ctor'
Error CS0656 Missing compiler required member 'System.Runtime.CompilerServices.RequiredMemberAttribute..ctor'

My workaround solution is using conditional compilation as:

 public class Person
    {
       
        public
#if NET7_0
            required
#endif
            int Id { get; set; }
    }

What should I do to support the new features 'required' of c# 11 in a multi target project has net47x?

M.Hassan
  • 10,282
  • 5
  • 65
  • 84

2 Answers2

2

Just like it was the case with some new language features in older C# versions, this will work when you create the missing attributes yourself. For the required keyword you will need this attributes:

But since this is not officially supported, there might be problems either now or in the future.

You can use the PolySharp package to get these (and other) classes in your projects.

cremor
  • 6,669
  • 1
  • 29
  • 72
  • Thanks for answer. I really have published a package [Compatibility.CompilerFeatures](https://github.com/moh-hassan/Compatibility.CompilerFeatures) since a week that support the required feature and plan to add extra features to support old frameworks. These classes are tested with old frameworks (check the test project and the demo) and It's safe with no side effect for using it in net4x. – M.Hassan Nov 22 '22 at 15:00
  • 1
    @M.Hassan FYI, there is also `CallerArgumentExpressionAttribute`. – cremor Nov 23 '22 at 06:20
  • Nice c# 10 feature. Welcome for contribution. – M.Hassan Nov 23 '22 at 10:56
  • 1
    @M.Hassan There is already another package that provides these (and other) classes as source files: [PolySharp](https://www.nuget.org/packages/PolySharp/) – cremor Jan 13 '23 at 06:28
1

C# 11 language features is definitely not supported in Framework 4.7.2, from this table: https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/configure-language-version it shows the default language version for NET Framework was C# 7.3, and this SO answer C# 8 was starting to have problems. C# 8 features in .NET Framework 4.7.2 As noted in the last link, some features may have been included, but was not officially supported.

If you absolutely have to multi-target, you either have to do your conditional compilation, or be mindful of what language features you use, limiting it to C# 7.3. You could try updating to NET Framework 4.8, but I think the most you will gain is support for C#8.

Anthony G.
  • 452
  • 2
  • 7