Why is this code throwing an InvalidOperationException?

As you can see here, the First method throws an InvalidOperationException when the sequence on which it is called is empty. Since no element of the result of the split equals Hello5, the result is an empty list. Using First on that list will throw the exception. Consider using FirstOrDefault, instead (documented here), which, instead of throwing an exception when the sequence is empty, returns … Read more