Reputation: 23
Why this code work
public static List<int> RemoveSmallest(List<int> numbers)
{
numbers.Remove(numbers.Min());
return numbers;
}
instead of this. What the reason?
public static List<int> RemoveSmallest1(List<int> numbers)
{
return numbers.Remove(numbers.Min());
}
Upvotes: 2
Views: 2351
Reputation: 3439
List<T>.Remove()
returns true if item is successfully removed; otherwise, false. This method also returns false if item was not found in the List<T>
.
In the method List<int> RemoveSmallest(List<int> numbers)
, return type is List<int>
not a bool, that is why 2nd approach gives an error.
If result is assigned to a variable like below:
var result = numbers.Remove(numbers.Min());
You can see result
type as well:
Upvotes: 2
Reputation: 164
List.Remove() returns true or false, depending on whether it was successful, not a List.
https://msdn.microsoft.com/en-us/library/cd666k3e(v=vs.110).aspx
Upvotes: 6