Search code examples
c#unity-game-enginelvalueienumerator

C# - require a returned type to be assigned to an L-Value?


this may not be sensible, but i'm looking at a situation where i would like it to be a compile error if the return value of a method goes unused.

specifically, Unity3D implements coroutines which look like this:

IEnumerator myCoroutine() {...  yield break;}

if you call myCoroutine() and do nothing with the returned value (eg, you're calling it like a normal method, not from inside another yield clause or via the StartCoroutine() method), the result is that all the code inside myCoroutine() is not executed. however, there is no compile- or runtime- warning. i would like to get a compile-time error in this scenario.

one approach i thought of is to subclass IEnumerator into say MyIEnumerator, which adds the property that it must be passed on in some way - either assigned to an L-Value or passed to a method, etc.

but i haven't been able to discover if such a requirement exists.


Solution

  • There is no such feature in C# for this. You'd need to create some form of 3rd party code analysis tool to try to look for cases such as these.