Avoid CS8618 warning when initializing mutable non nullable property with argument validation

For now you can avoid this warning by initializing a _name field using default value with null-forgiving operator !, like private string _name = default!; or private string _name = null!; There is also an open GitHub issue for that. You can also declare the _name as string? and specify that return value of Name … Read more

What does “is { }” mean?

That’s just the empty property pattern in C# 8, meaning the value not null. It matches any value type or reference type. As Panagiotis Kanavos notes in the comments, this is equivalent to the good old value is object check which has been in C# for a long time. Generally if you were to specify … Read more

Nullable reference types: How to specify “T?” type without constraining to class or struct

What to do if you are using C# 9 In C# 9, you can use T? on an unconstrained type parameter to indicate that the type is always nullable when T is a reference type. In fact, the example in the original question “just works” after adding ? to the property and constructor parameter. See … Read more

How to treat ALL C# 8 nullable reference warnings as errors?

It is now possible to treat all nullable-related warnings as errors without explicitly specifying them all. To achieve this, you have to set <WarningsAsErrors>Nullable</WarningsAsErrors> in your *.csproj file [source]. Full example: <Project Sdk=”Microsoft.NET.Sdk.Web”> <PropertyGroup> <TargetFramework>netcoreapp3.1</TargetFramework> <Nullable>enable</Nullable> <WarningsAsErrors>Nullable</WarningsAsErrors> </PropertyGroup> </Project>

Nullable reference types with generic return type

You were very close. Just write your method like this: [return: MaybeNull] public T Get<T>(string key) { var wrapper = cacheService.Get(key); return wrapper.HasValue ? Deserialize<T>(wrapper) : default!; } You have to use the default! to get rid of the warning. But you can tell the compiler with [return: MaybeNull] that it should check for null … Read more

When does IDE0063 dispose?

You are using C# 8. In older C# versions that ; would have made this invalid. In the new syntax, the client stays in scope for the surrounding method (or other {} scope block). Note that you can omit the outer pair of () as well. It’s called a using declaration, the documentation is here. … Read more

Convert IAsyncEnumerable to List

Sure – you just need the ToListAsync() method, which is in the System.Linq.Async NuGet package. Here’s a complete example: Project file: <Project Sdk=”Microsoft.NET.Sdk”> <PropertyGroup> <OutputType>Exe</OutputType> <TargetFramework>netcoreapp3.1</TargetFramework> </PropertyGroup> <ItemGroup> <PackageReference Include=”System.Linq.Async” Version=”4.0.0″ /> </ItemGroup> </Project> Code: using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; class Program { static async Task Main(string[] args) { IAsyncEnumerable<string> sequence = … Read more

How to enable Nullable Reference Types feature of C# 8.0 for the whole project

In Visual Studio 16.2 (from preview 1) the property name is changed to Nullable, which is simpler and aligns with the command line argument. Add the following properties to your .csproj file. <PropertyGroup> <Nullable>enable</Nullable> <LangVersion>8.0</LangVersion> </PropertyGroup> If you’re targeting netcoreapp3.0 or later, you don’t need to specify a LangVersion to enable nullable reference types. Alternatively, … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)