In C#, I can use the yield keyword to implement a generator, viz:
int GenInt()
{
for(int i = 0; i < 5; i++)
yield return i;
}
Then, calling the function multiple times will return 0 through 4.
Can the same thing be done in C++/CLI? There’s no yield keyword, so my gut reaction is that there isn’t, which sucks, but what can you do?
yield returnin C# is just a shortcut that lets the compiler generate the necessary code for you that implements an implementation ofIEnumerable<T>andIEnumerator<T>. Since C++/CLI doesn’t offer this service, you’ve got to do it manually: just write two classes, one that implements each interface (or, doing it like the C# compiler, one class implementing both but this can get messy if the whole thing can be called repeatedly – cue: statefulness).Here’s a small example – since I don’t have an IDE and my C++/CLI is a bit rusty, I’ll give it in C#: