• he/him

Coder, pun perpetrator
Grumpiness elemental
Hyperbole abuser


Tools programmer
Writer-wannabe
Did translations once upon a time
I contain multitudes


(TurfsterNTE off Twitter)


Trans rights
Black lives matter


Be excellent to each other


UE4/5 Plugins on Itch
nte.itch.io/

eniko
@eniko

Have you ever wanted to write an IEnumerable method in C# cause yielding is massively powerful, but don't like that it means allocating every time you spin up that method? Here's a neat trick you can do: wrap your IEnumerable in a class, and put a reset bool field on it. Then put a label at the start of your method which sets reset to false, and the following after every yield:

if (reset) goto routine_start;

Then at the end just yield forever while checking reset to start over. You can even get fancy about it and have the wrapper class implement IEnumerable and an enumerator struct which wraps around the actual method. Then you can yield a value at the end like null to indicate that the method is over and the enumerator struct can end. That way you can create an instance of the wrapper class once, foreach on it like normal, and never allocate beyond the initial allocation.


You must log in to comment.