Is there ever a reason to not use 'yield return' when returning an IEnumerable?

后端 未结 5 716
北海茫月
北海茫月 2020-12-09 16:16

Simple example - you have a method or a property that returns an IEnumerable and the caller is iterating over that in a foreach() loop. Should you always be using

5条回答
  •  醉梦人生
    2020-12-09 16:46

    Without reading through answers I will tell you that using yield in a WCF service will mess up your FaultException!

    It took me quite some time to find out that yield was the culprit. I just needed a fast way too get som errors up n running. The proper way would of course be to include an Error object in the reply but I needed it fast. I did : throw new FaultException("No subscription with id: " + subscriptionId.ToString());

    But using yield, even below the actuall throwing, caused a regular exception, network

提交回复
热议问题