Last active
April 19, 2021 08:51
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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