Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does IEnumerable from yielding can ever be null?

Does an IEnumerable obtained from a yield expression can ever be null?

like image 390
Guillermo Gutiérrez Avatar asked May 18 '26 02:05

Guillermo Gutiérrez


1 Answers

No. The C# compiler converts your iterator block into an iterator instance, which is what is returned from your method. This can never be null.

That said, it is of course possible to yield null values within your enumeration, provided that the generic type of your enumerable is a reference type or a nullable type.

like image 100
Douglas Avatar answered May 19 '26 16:05

Douglas