:keyword_null: null :keyword_async: async :concept_method: method :typeparameter_TResult: == Why is this an issue? include::../why-dotnet.adoc[] [source,csharp] ---- public Task DoFooAsync() { return null; // Noncompliant: Causes a NullReferenceException if awaited. } public async Task Main() { await DoFooAsync(); // NullReferenceException } ---- == How to fix it include::../how-to-fix-it-dotnet.adoc[] === Code examples A `Task` returning {concept_method} can be fixed like so: ==== Noncompliant code example [source,csharp,diff-id=1,diff-type=noncompliant] ---- public Task DoFooAsync() { return null; // Noncompliant: Causes a NullReferenceException if awaited. } ---- ==== Compliant solution [source,csharp,diff-id=1,diff-type=compliant] ---- public Task DoFooAsync() { return Task.CompletedTask; // Compliant: Method can be awaited. } ---- A `Task{typeparameter_TResult}` returning {concept_method} can be fixed like so: ==== Noncompliant code example [source,csharp,diff-id=2,diff-type=noncompliant] ---- public Task GetFooAsync() { return null; // Noncompliant: Causes a NullReferenceException if awaited. } ---- ==== Compliant solution [source,csharp,diff-id=2,diff-type=compliant] ---- public Task GetFooAsync() { return Task.FromResult(null); // Compliant: Method can be awaited. } ---- include::../resources.adoc[] * C# Language Design - https://github.com/dotnet/csharplang/issues/35[Proposal Champion "Null-conditional await"] include::../rspecator.adoc[]