Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# 8.0 notnull Generic Type Contraint

Tags:

c#

c#-8.0

I'm trying to use the just-released .NET Standard 2.1 C# 8.0 features in VS2019 Preview 16.4.0 but keep receiving the message "Cannot resolve symbol 'notnull'".

My example code:

class Example<T> where T : notnull {}

I have the <Nullable>enable</Nullable> and <LangVersion>8</LangVersion> property set in my project file and all of that new functionality is working great, it just seems to be this.

How can I use the notnull type contraint?

like image 519
Benjamin Avatar asked Mar 21 '26 00:03

Benjamin


1 Answers

This is because C# 8.0 is not enabled.

In some preview versions of .NET Core 3.0 SDK, C# 8.0 was a preview version rather than latest or latestMajor version which is taken as the default language version.

But at least in .NET Core 3.0 GA (formal release version), C# 8.0 is enabled by default if your project targets netcoreapp3.0. But as for netstandard2.1 target, the default language version is still 7.3.

If you really need to share this library with the latest Xamarin apps that supports .NET Standard 2.1, specify <LangVersion>8.0</LangVersion> explicitly in the .csproj file within PropertyGroup, or you just set the TargetFramework to netcoreapp3.0 and C# 8.0 should be enabled by default.

like image 132
Alsein Avatar answered Mar 23 '26 16:03

Alsein



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!