I have some IEnumberable
collection of items. I use .Single()
to find a specific object in the collection.
I choose to use Single()
I wouldn't recommend using the try/catch in this scenario, because using exceptions to make logical decisions is resource expensive.
I would recommend using SingleOrDefault()
, and check if the result is null. If it is. Do your creation.
What you want is SingleOrDefault()
The "or default" actually means it returns null (for reference types) or whatever the default would be for a non-reference type. You'll need to new-up an object to take its place.