Skip to content

Instantly share code, notes, and snippets.

@skalahonza
Last active April 19, 2021 08:51
Show Gist options
  • Save skalahonza/8451f0fea118d8538e6bcb22dedf85c9 to your computer and use it in GitHub Desktop.
Save skalahonza/8451f0fea118d8538e6bcb22dedf85c9 to your computer and use it in GitHub Desktop.
Async methods cannot use ref or out parameters. You can however use this Ref wrapper which is valid in terms of syntax
public class Ref<T>
{
public Ref() { }
public Ref(T value) { Value = value; }
public T Value { get; set; }
public override string ToString()
{
T value = Value;
return value == null ? "" : value.ToString();
}
public static implicit operator T(Ref<T> r) { return r.Value; }
public static implicit operator Ref<T>(T value) { return new Ref<T>(value); }
}
// usage
public async IAsyncEnumerable<JObject> GetAllCounted(Ref<int> count)
{
await foreach(var item in GetAll())
{
count.Value++;
yield return item;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment