Can a null literal be expected?

You can wait for something in C # 5+ as long as the type has a function called .GetAwaiter() that returns a special object or has an extension method that does the same.

Unity3d in the next release will support async / wait. Currently in Unity3d you can yield return null; inside the coroutine, submit "Wait for the next frame."

I was wondering if it is possible to create an extension method that allows you to make await null; to get the same behavior. No System.Null , for example, there is System.Void , so I could not figure out which type should be used in the extension method.

+7
c # asynchronous async-await
source share
2 answers

There is no way to define an extension method for a null literal, since literal null not of type :

..we realized that the null type was strange. Is it a single value type - or is it? Is the null nullable int value really the same as the value of a null string? And dont the values ​​of type nullable type already have a type, namely the type of the value NULL?

and

Therefore, we removed references to the useless "null type" in the C # 3.0 specification.

+3
source share

You are right that profitability and expectation are closely related. Both are points in the workflow where the current method is paused, control returns to the caller, and the method resumes at an undefined point in the future at the point where the exit / wait occurred.

But they differ from each other in terms of their effect on their operands. They are actually doubles of each other. The output gives a new meaning when a code that iterates a sequence is required. Waiting retrieves a value when it is created by an asynchronously executed task.

Null is an absolutely acceptable value, so it makes sense to get income to its caller. But null is not a valid task, so it makes no sense to expect you to extract the value from the task.

Currently in Unity3d you can return return null; inside the coroutine to represent "Wait for the next frame."

In an asynchronous workflow with asynchronous wait, the counterpart is yield return null; is only return null; . This means that "complete this part of the asynchronous workflow by providing a null reference." Why are you not just returning null if you are going to create a task whose result is null?

Let me say one more way that may be clearer. Obviously this doesn't make sense:

 foreach(var x in null) Console.WriteLine(x); 

This is identically pointless:

 var x = await null; Console.WriteLine(x); 

This is the same logical. Foreach means “retrieve a value from a sequence until values ​​are available,” but null is not a sequence. Similarly, “wait” means “extract a value from a task as soon as a value is available”, but null is not a task.

What is the key: the asynchronous analogue of await not yield return , it is foreach . This is the mechanism that extracts T from IEnumerable<T> or Task<T> . What puts T in the monadic type is yield return for IEnumerable<T> and return for Task<T> .

+7
source share

All Articles