66 lines
1.5 KiB
Plaintext
66 lines
1.5 KiB
Plaintext
include::../../../shared_content/dotnet/vbnet_dictionary.adoc[]
|
|
|
|
== Why is this an issue?
|
|
|
|
include::../why-dotnet.adoc[]
|
|
|
|
[source,vbnet]
|
|
----
|
|
Public Function DoFooAsync() As Task
|
|
Return Nothing ' Noncompliant: Causes a NullReferenceException if awaited.
|
|
End Function
|
|
|
|
Public Async Function Main() As Task
|
|
Await DoFooAsync() ' NullReferenceException
|
|
End Function
|
|
----
|
|
|
|
== 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,vbnet,diff-id=1,diff-type=noncompliant]
|
|
----
|
|
Public Function DoFooAsync() As Task
|
|
Return Nothing ' Noncompliant: Causes a NullReferenceException if awaited.
|
|
End Function
|
|
----
|
|
|
|
==== Compliant solution
|
|
|
|
[source,vbnet,diff-id=1,diff-type=compliant]
|
|
----
|
|
Public Function DoFooAsync() As Task
|
|
Return Task.CompletedTask ' Compliant: Method can be awaited.
|
|
End Function
|
|
----
|
|
|
|
A `Task{typeparameter_TResult}` returning {concept_method} can be fixed like so:
|
|
|
|
==== Noncompliant code example
|
|
|
|
[source,vbnet,diff-id=2,diff-type=noncompliant]
|
|
----
|
|
Public Function GetFooAsync() As Task(Of Object)
|
|
Return Nothing ' Noncompliant: Causes a NullReferenceException if awaited.
|
|
End Function
|
|
----
|
|
|
|
==== Compliant solution
|
|
|
|
[source,vbnet,diff-id=2,diff-type=compliant]
|
|
----
|
|
Public Function GetFooAsync() As Task(Of Object)
|
|
Return Task.FromResult(Of Object)(Nothing) ' Compliant: Method can be awaited.
|
|
End Function
|
|
----
|
|
|
|
include::../resources.adoc[]
|
|
|
|
include::../rspecator.adoc[] |