How to implement an efficient WhenEach that streams an IAsyncEnumerable of task results?

By using code from this article, you can implement the following: public static Task<Task<T>>[] Interleaved<T>(IEnumerable<Task<T>> tasks) { var inputTasks = tasks.ToList(); var buckets = new TaskCompletionSource<Task<T>>[inputTasks.Count]; var results = new Task<Task<T>>[buckets.Length]; for (int i = 0; i < buckets.Length; i++) { buckets[i] = new TaskCompletionSource<Task<T>>(); results[i] = buckets[i].Task; } int nextTaskIndex = -1; Action<Task<T>> continuation … Read more

.net core 3 yields different floating point results from version 2.2

.NET Core introduced a lot of floating point parsing and formatting improvements in IEEE floating point compliance. One of them is IEEE 754-2008 formatting compliance. Before .NET Core 3.0, ToString() internally limited precision to “just” 15 places, producing string that couldn’t be parsed back to the original. The question’s values differ by a single bit. … Read more

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

The annotation for nullable reference types should only be used in code within a ‘#nullable’ context

For anyone ending up here. You can put #nullable enable on top of the file for a file-by-file approach as suggested by @Marc in the comments. You can also use combinations of #nullable enable/disable to annotate just parts of the file class Program { static void Main(string[] args) { #nullable enable string? message = “Hello … Read more

Default implementation in interface is not seen by the compiler?

Methods are only available on the interface, not the class. So you can do this instead: IJsonAble request = new SumRequest() var result = request.ToJson(); Or: ((IJsonAble)new SumRequest()).ToJson(); The reason for this is it allows you to add to the interface without worrying about the downstream consequences. For example, the ToJson method may already exist … 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