ARTICLE AD BOX
I'm trying wrap my head around this issue,
I've created a sample C# program.
myClassObj is deliberately set to null to produce this scenario.
Program fails at await myClassObj?.MethodAsync()
Expectation was that it will handle null scenario as it does in case of action, myActionObj?.Invoke() does not fail.
Curious to know why this is?
using System; using System.Threading.Tasks; public class MyClass { public async Task MethodAsync() { await Task.Delay(1000); } } public class Program { public async static Task Main() { Console.WriteLine("Start"); Action myActionObj = null; MyClass myClassObj = null; try { myActionObj?.Invoke(); // works await myClassObj?.MethodAsync(); // doesn't work } catch (Exception e) { Console.WriteLine($"{e}"); } Console.WriteLine("End"); } }