2012-06-01 43 views
2
public SPListItemCollection GetACollection() 
{ 
    try 
    { 
     //Method to get an item collection from somewhere 
     if(itemCol != null) 
      return itemCol; 
     else 
      return null; 
    } 
    catch(Exception ex) 
    { 
     LogException(ex); 
     return null; 
    } 
} 

此方法必須返回null(不是空列表) - 無論是否捕獲到異常。上述作品 - 但有沒有更好的方法呢?從try ...中返回null的方法... catch

+5

我只是不明白你爲什麼需要'if-else'。 – Leri

+0

你可以在try中做:return itemCol;而不是if/else。 – RvdK

+2

如果'itemCol'是一個_empty_列表(包含0個項目的集合實例),它仍然會被返回。 –

回答

1

你提到你目前的實施工作。我假定用於獲取項目集合的方法返回包含1個或更多項目的集合,或者返回null或引發異常。如果這是真的,這將是一個選擇。

public SPListItemCollection GetACollection() 
{ 
    SPListItemCollection itemCol = null; 
    try 
    { 
     itemCol = //Method to get an item collection from somewhere 
    } 
    catch(Exception e) 
    { 
     LogException(e); 
    } 
    return itemCol; 
} 
0

它已經很好,海事組織,我也會刪除最後一個else,像這樣:

public SPListItemCollection GetACollection() 
{ 
    try 
    { 
     //Method to get an item collection from somewhere 
     if(itemCol != null) 
      return itemCol; 

     //Some if/else code here presumably... 

     //NO ELSE HERE... 
     return null; 
    } 
    catch(Exception ex) 
    { 
     LogException(ex); 
     return null; 
    } 
} 
+4

我會刪除'if'以及:) – dasblinkenlight

+0

@Tigran如果'itemCol'爲null,這沒有意義'return itemColl'將與'return null'相同。 – Leri

+0

@dasblinkenlight:我**假設**中間還有其他一些代碼。剛剛建議謹慎。 – Tigran

4
if(itemCol != null) 
    return itemCol; 
else 
    return null; 

簡化爲

return itemCol; 

因爲如果itemCol == null然後返回itemCol已經返回null。沒有理由特殊情況下的行爲。

如果您想通過null更換空的集合,你需要使用類似:

if((itemCol != null) && itemCol.Any()) 
    return itemCol; 
else 
    return null; 

設計諮詢了一下:

  • 吞嚥異常氣餒。您應該只捕獲一些您知道如何處理的特定例外
  • 空集合通常比null更好。
+0

那正是我剛剛發佈的內容! LOL :-) – trumpetlicks

+0

+1注意吞嚥異常不鼓勵 –

4

我必須先說這是一件可怕的事情,吞嚥異常是可怕的!不要這樣做!它會困擾你並且使得調試非常困難,甚至更糟的是由於異常而返回null - null通常會拋出一個NullReferenceException,這將比正在被吞下的異常(即使它被記錄)更難調試,但既然你問:

public SPListItemCollection GetACollection() 
{ 
    SPListItemCollection itemCol = null; 
    try 
    { 
     //Method to get an item collection from somewhere 
    } 
    catch(Exception ex) 
    { 
     LogException(ex); 
    } 
    return itemCol; 
} 
0

你不需要if-else塊,因爲你已經返回null如果對象爲空:

public SPListItemCollection GetACollection() 
{ 
    try 
    { 
     //Method to get an item collection from somewhere 
     return itemCol; 
    } 
    catch(Exception ex) 
    { 
     LogException(ex); 
     return null; 
    } 
} 
1

要檢查天氣的集合是空的,Any()方法是有用的,主要是如果你沒有列表,但一般的IEnumerables。 如果你只是想擺脫return null;重複的,你可以很容易做到:

public SPListItemCollection GetACollection() 
{ 
    try 
    { 
     //Method to get an item collection from somewhere 
     if(itemCol.Any()) 
      return itemCol; 
    } 
    catch(Exception ex) 
    { 
     LogException(ex); 
    } 

    return null; 
} 

您也可以將您的catch的右括號後直接追加

finally 
    { 
     // Stuff that always needs to be done. 
    } 

+0

刪除'if(itemCol!= null)'這一行,我給你一個+1。 –

+0

這就是我看到其他答案後正在做什麼;) – linac

0

這個怎麼樣?如果你的規範說:

必須返回null(非空列表)

您當前的實現將返回一個空的集合,如果集合不爲空,但空。除非你依靠這種方法來處理它。

這需要照顧那個......

public SPListItemCollection GetACollection() 
{ 
    try 
    { 
     //Method to get an item collection from somewhere 
     if(itemCol != null && itemCol.Count == 0) 
      return itemCol; 

     return itemCol; 
    } 
    catch(Exception ex) 
    { 
     LogException(ex); 
    } 

    return null; 
} 
3

從來不喜歡用多出口點的方法,
在你設置itemCol爲null,則的try/catch外返回itemCol

try 
{ 
    // whatever 
    if(itemCol.Count == 0) itemCol = null; 
} 
catch(Exception x) 
{ 
    LogException(x); 
    itemCol = null; 
} 
return itemCol; 
+0

這將返回空列表不爲空 – Reniuz

+0

@Reniuz權利,需要在嘗試塊中檢查 – Steve

0

我會這麼做:

public SPListItemCollection GetACollection() 
{ 
    try 
    { 
     //Method to get an item collection from somewhere 
     return itemCol != null && itemCol.Count == 0 ? null : itemCol; 
    } 
    catch(Exception ex) 
    { 
     LogException(ex); 
     return null; 
    } 
} 
0

你的代碼看起來不錯。如果它適合你的口味更好,你可以把最後的零回報:

public SPListItemCollection GetACollection() 
{ 
    try 
    { 
     //Method to get an item collection from somewhere 
     if(itemCol != null) 
      return itemCol; 
    } 
    catch(Exception ex) 
    { 
     LogException(ex); 
    } 
    return null; 
}