I am safely browsing the list for such an object:
var someResult = myList.FirstOrDefault(x=>x.SomeValue == "SomethingHere");
If there are no objects matching my criteria, then it someResultwill be null.
But if I only have the index of the object I want, things will not be so good. It seems to me something like this:
try
{
var someResult = myList[4];
}
catch (ArgumentOutOfRangeException)
{
someResult = null;
}
I admit that it's not scary to write. But it seems to me that there should be a way to simply return a null list if the index ends with a fake.
Is one (or two) lines sent using existing .net methods ?
(I know that I could easily write an extension method, but I wonder if there is a built-in way to do this.)
source
share