「並非所有的代碼路徑返回一個值」這個塊爲什麼會拋出錯誤?
public BallData GetBall(String Name)
{
//Check each item in the list for the name.
foreach (BallData Item in _BallList)
{
//If the name matches, return the item to the caller and exit the loop.
if (Item.Name == Name)
{
return Item;
}
else
{
// Otherwise, throw an exception to indicate that the ball wasn't found.
throw new KeyNotFoundException("The ball name doesn't exist.");
}
}
}
你有沒有意識到這樣只會看第一在返回/拋出之前'_BallList'中的項? –