Why does Task implement IAsyncResult, but does not contain an AsyncWaitHandle member?

Perhaps this is a stupid question.

The class is Taskdeclared as follows:

public class Task : IThreadPoolWorkItem, IAsyncResult, IDisposable

An interface is IAsyncResultdeclared as follows:

public interface IAsyncResult
{
    object AsyncState { get; }
    WaitHandle AsyncWaitHandle { get; }
    bool CompletedSynchronously { get; }
    bool IsCompleted { get; }
}

But the member AsyncWaitHandledoes not exist in the class or instances Task.

This code:

System.Threading.Tasks.Task t = new System.Threading.Tasks.Task(() => { }); 
t.AsyncWaitHandle.ToString(); 

Causes a compilation error:

Error 1 'System.Threading.Tasks.Task' does not contain a definition for "AsyncWaitHandle" and no extension method "AsyncWaitHandle" Accepting the first argument of the type "System.Threading.Tasks.Task" may (do you miss the use directive or assembly reference? )

However, this is not only compiled:

System.IAsyncResult t = new System.Threading.Tasks.Task(() => { }); 
t.AsyncWaitHandle.ToString(); 

But also works, since the member exists. What kind of witchcraft is this?

Is this a compiler trick or is it hidden differently?

Greetings.

+4
2

Task IAsyncResult, :

System.Threading.Tasks.Task t = new System.Threading.Tasks.Task(() => { }); 
((IAsyncResult)t).AsyncWaitHandle.ToString()

:

public class Task : IAsyncResult
{
    WaitHandle IAsyncResult.AsyncWaitHandle
    {
        get { ... }
    }
}
+6

msdn .

http://msdn.microsoft.com/en-us/library/ms173157.aspx

IControl.Paint IControl ISurface.Paint ISurface. , .

interface IControl
{
    void Paint();
}
interface ISurface
{
    void Paint();
}
class SampleClass : IControl, ISurface
{
    void IControl.Paint()
    {
        System.Console.WriteLine("IControl.Paint");
    }
    void ISurface.Paint()
    {
        System.Console.WriteLine("ISurface.Paint");
    }
}

, .

+2

Source: https://habr.com/ru/post/1530028/


All Articles