In C#, why can't an anonymous method contain a yield statement?
I thought it would be nice to do something like this (with the lambda doing a yield return): public IList<T> Find<T>(Expression<Func<T, bool>> expression) where T : class, new() { IList<T> list = GetList<T>(); var fun = expression.Compile(); var items = () => { foreach (var item in list) if (fun.Invoke(item)) yield return item; // This is not allowed by C# } return items.ToList(); } However, I found out that I can't use yield in anonymous method. I'm wondering why. The yield docs just say it is not allowed. Since it wasn't allowed I just created List and added the items to it. Eric Lippert