What is the difference between using and await using? And how can I decide which one to use?

Classic sync using Classic using calls the Dispose() method of an object implementing the IDisposable interface. using var disposable = new Disposable(); // Do Something… Is equivalent to IDisposable disposable = new Disposable(); try { // Do Something… } finally { disposable.Dispose(); } New async await using The new await using calls and await the … Read more

Can a C# class call an interface’s default interface method from its own implementation?

As far as I know you can’t invoke default interface method implementation in inheriting class (though there were proposals). But you can call it from inheriting interface: public class HappyGreeter : IGreeter { private interface IWorkAround : IGreeter { public void SayHello(string name) { (this as IGreeter).SayHello(name); System.Console.WriteLine(“I hope you’re doing great!!”); } } private … Read more

Default Interface Methods. What is deep meaningful difference now, between abstract class and interface?

Conceptual First of all, there is a conceptual difference between a class and an interface. A class should describe an “is a” relationship. E.g. a Ferrari is a Car An interface should describe a contract of a type. E.g. A Car has a steering wheel. Currently abstract classes are sometimes used for code reuse, even … Read more

Factory for IAsyncEnumerable or IAsyncEnumerator

Here is another implementation of the AsyncEnumerableSource class, that doesn’t depend on the Rx library. This one depends instead on the Channel<T>, class, which is natively available in the .NET standard libraries. It has identical behavior to the Rx-based implementation. The class AsyncEnumerableSource can propagate notifications to multiple subscribers. Each subscriber can enumerate these notifications … Read more

What are Range and Index types in C# 8?

They’re used for indexing and slicing. From Microsoft’s blog: Indexing: Index i1 = 3; // number 3 from beginning Index i2 = ^4; // number 4 from end int[] a = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; Console.WriteLine($”{a[i1]}, {a[i2]}”); // “3, 6” Range (slicing): We’re also introducing a Range … 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. For … Read more