Here is an interesting example. Consider the following function:
public static IEnumerable<int> Fun() { if (false) { yield return 0; } }
A line with yield is defined as unreachable. However, deleting this file will make the program uncompiled. yield contained in this function gives the compiler information to reorganize the function, so it does nothing, it simply returns an empty collection. When you delete the yield line, it looks the same as a regular function without returning when it is required.
As in the commentary, this example is contrived, but instead of false we could have a constant value from another generated project, etc. (i.e. such a piece of code would not look as obvious as in this case).
Edit : Note: the yield construct is actually very similar to async/await . However, with the latest creators of the language, a different approach (IMO) has been taken that prevents such scenarios. Iterator blocks can be defined in the same way (using some keyword in the function signature instead of detecting it from the function body).
source share