我不能做我想做的事。我只想要非國際代表的帳戶。但是當我調用ActiveAccounts()時,我沒有變爲空,我得到一個可枚舉的,然後包含null。我在這裏做錯了什麼?請幫助。爲什麼這種擴展方法不起作用?
public static class AccountExt
{
public static IEnumerable<Account> ActiveAccounts(this AccountRep rep)
{
if(rep == null)
throw new ArgumentNullException();
if(rep.IsInternational)
yield return null;
foreach(var acc in rep.FetchAccounts())
{
if(acc.IsActive)
yield return acc;
}
}
}
沒錯。我討厭它,當返回任何類型的枚舉的函數返回null。它的代碼混亂。 – 2010-02-12 00:26:55
即使有'yield break',OP也可能希望將該方法拆分爲兩個,以便前提條件可以立即進行計算,而不必在枚舉器迭代時進行計算。這可能會導致意外的異常遠離實際的故障點。 – LBushkin 2010-02-12 00:29:22
我需要ActiveAccounts()來返回null。空的可擴展性不是我需要的,所以會產生突破性工作? – msophie 2010-02-12 00:31:59