question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Compiler error (Init-only setters) when using LangVersion 8.0 in a .NET Standard 2.0 library

See original GitHub issue

Description (optional)

I got compiler error CS8400 Feature 'init-only setters' is not available in C# 8.0. Please use language version 9.0 or greater. when compiling a .NET Standard 2.0 class library with C# 8.0 as the LangVersion. Upgrading to 9.0 fixed the issue but should not be nesessary.

image

Reproduction Steps

Create a Class Library using the following .csproj content and this class Product.cs

<Project Sdk="Microsoft.NET.Sdk">
	<PropertyGroup>
		<TargetFramework>netstandard2.0</TargetFramework>
		<LangVersion>8.0</LangVersion>
		<Nullable>enable</Nullable>
	</PropertyGroup>
	<ItemGroup>
	  <PackageReference Include="PolySharp" Version="1.6.0" />
	</ItemGroup>
</Project>
using System.Diagnostics.CodeAnalysis;

namespace ClassLibrary19 {
    public class Product {

        [AllowNull]
        public string ProductName { get; set; }
    }
}

Expected Behavior

No compiler error

Actual Behavior

error CS8400

System info

This section should contain useful info such as:

  • PolySharp NuGet version 1.6

Additional context (optional)

Any additional info that might be useful to resolve the issue.

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
sonnemafcommented, Oct 27, 2022

Thank you! It is really great to have this. I was using this Nullable package before. But PolySharp has much more. Happy!

0reactions
sonnemafcommented, Oct 27, 2022

Thanks for this tip. I will set the PrivateAssets=“all”.

Read more comments on GitHub >

github_iconTop Results From Across the Web

After update to VS 16.9.0: Feature 'init-only setters' is not ...
This looks to be a badly-advised breaking change in the library, that was added at the end of January and which has already...
Read more >
C# 9 Records and Init Only Settings Without .NET 5
To use a public or protected property with an init only setter, the consumer must be using a C# compiler that supports C#...
Read more >
Why isn't .NET Standard 2.0 used as the default compared ...
I know that .NET Framework and .NET Core are the implementation frameworks, while .NET Standard 2.0 is the specification that the frameworks ...
Read more >
Init Properties and modreq · Issue #3376 · dotnet/csharplang
The plan is to have the compilers (C#, VB and F#) that ship with .NET 5 to be able to consume init-only properties...
Read more >
Init only setters - C# 9.0 draft feature specifications
This feature specification describes the rules for 'init' only setters in properties. These can be set only as part of an instantiation, ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found