Skip to content

Instantly share code, notes, and snippets.

@pakrym
Created August 3, 2017 23:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pakrym/e9fc0a6babfaa259fc79310a53a643f0 to your computer and use it in GitHub Desktop.
Save pakrym/e9fc0a6babfaa259fc79310a53a643f0 to your computer and use it in GitHub Desktop.
using System;
using System.Threading.Tasks;
using System.Runtime.CompilerServices;
namespace refreturnawaitable
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
new C().M().Wait();
}
}
public class C {
public A _a;
public async Task M() {
_a = new A(this);
await DoAsync();
Console.WriteLine(_a._i);
}
public ref A DoAsync()
{
return ref _a;
}
}
public struct A: ICriticalNotifyCompletion
{
private C _c;
public int _i;
public A(C c)
{
_c = c;
_i = 0;
}
public ref A GetAwaiter(){
return ref _c._a;
}
public void UnsafeOnCompleted(Action continuation)
{
OnCompleted(continuation);
}
public void OnCompleted(Action continuation)
{
_i ++;
continuation();
}
public bool IsCompleted => false;
public void GetResult()
{
_i ++;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment