Mark strings as non-nullable in ASP.NET Core 3.0 Swagger

You can now use nullable reference types to mark strings as optional or required: Enable nullable reference types for your project/solution Mark strings in Dtos as nullable: public class ExampleDto { [StringLength(64, MinimumLength = 4)] public string RequiredString { get; set; } [StringLength(200)] public string? OptionalString { get; set; } } Enable Swagger support in … Read more

Multiple statements in a switch expression: C# 8 [duplicate]

Your only supported choice is the func like you did. See [this article][1] for more information. His example: var result = operation switch { “+” => ((Func<int>)(() => { Log(“addition”); return a + b; }))(), “-” => ((Func<int>)(() => { Log(“subtraction”); return a – b; }))(), “/” => ((Func<int>)(() => { Log(“division”); return a / … Read more

Return IAsyncEnumerable from an async method

The struct approach wouldn’t work. If you want to asynchronously return an IAsyncEnumerator<T> value, you could use Task<IAsyncEnumerator<T>> with return Bar();. However, that would be unusual. It would be much more natural to create a new IAsyncEnumerator<T> that incorporates await SomeAsyncMethod() at the beginning of the asynchronous enumerable. To do this, you should use await … Read more

Do C# 8 default interface implementations allow for multiple inheritance

Your question is answered by Mads Torgersen in the blog post you linked to: Actually interfaces are still quite far from abstract classes. Classes don’t inherit members from interfaces, so if a class leaves a member M implemented by the interface, the class does not have a member M! It’s like an explicit implementation today; … Read more

Calling C# interface default method from implementing class

See the documentation at https://learn.microsoft.com/en-us/dotnet/csharp/tutorials/default-interface-members-versions That cast from SampleCustomer to ICustomer is necessary. The SampleCustomer class doesn’t need to provide an implementation for ComputeLoyaltyDiscount; that’s provided by the ICustomer interface. However, the SampleCustomer class doesn’t inherit members from its interfaces. That rule hasn’t changed. In order to call any method declared and implemented in the … Read more

C# compiler throws Language Version (LangVersion) reference error “Invalid ‘nullable’ value: ‘Enable’ for C# 7.3”

In my case, I ran into this problem with Visual Studio 2022 when I changed the target framework from .NET Standard 2.1 to .NET Standard 2.0. I solved my problem by removing <Nullable>enable</Nullable> in the .csproj file and restarting Visual Studio. Original .csproj file: <PropertyGroup> <TargetFramework>netstandard2.1</TargetFramework> <Nullable>enable</Nullable> </PropertyGroup> New .csproj file: <PropertyGroup> <TargetFramework>netstandard2.0</TargetFramework> </PropertyGroup>

Why do we get possible dereference null reference warning, when null reference does not seem to be possible?

This is effectively a duplicate of the answer that @stuartd linked, so I’m not going to go into super deep details here. But the root of the matter is that this is neither a language bug nor a compiler bug, but it’s intended behavior exactly as implemented. We track the null state of a variable. … Read more

Nullable reference type in C#8 when using DTO classes with an ORM

You can do either of the following: EF Core suggests initializing to null! with null-forgiving operator public string ServiceUrl { get; set; } = null! ; //or public string ServiceUrl { get; set; } = default! ; Using backing field: private string _ServiceUrl; public string ServiceUrl { set => _ServiceUrl = value; get => _ServiceUrl … Read more

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