"并非所有代码路径都返回值"
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.");
}
}
}
Run Code Online (Sandbox Code Playgroud)
将您的代码更改为:
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;
}
}
throw new KeyNotFoundException("The ball name doesn't exist.");
Run Code Online (Sandbox Code Playgroud)