Please add these to System.Linq.Enumerable
```C#
public static T? FirstOrNull
public static T? LastOrNull
public static T? ElementAtOrNull
// and so on
```
I assume that this would be equivalent to var x = list.Cast<int?>().FirstOrDefault();
I think that FirstOrNull is a better name.
@Grauenwolf Not sure but I think implementation is even easier than that
```C#
public static T? FirstOrNullable
{
var iter = items.GetEnumerator();
if(!iter.MoveNext())
return (T?)null;
return iter.Current;
}
```
I don't have naming preference just only want this functionality
FirstOrNull makes much more sense. 'Nullable' includes both null and values.
I have *OrNull extension methods that travel around with me for pretty much every *OrDefault BCL API, e.g. GetValueOrNull(TKey key) on IReadOnlyDictionary.
@Grauenwolf no it would be faster, Cast<T?> is slow. I vote for FirstOrNull, LastOrNull etc.
@dark2201 - I'm pretty sure the intent was that the _behavior_ was the same.
Although it's true that adding an extra operator adds processing time, I'm not sure that a First case is going to really make a difference. Last, though, will need to convert everything before it can return any result.
While this would be awesome, I do think it should be broader than just structs. It is something that would be helpful for all value types. ints, bools, or even enums. 100% vote for this, but for all value types.
An int or boolean is a struct in .NET. You're probably thinking of C.
Huh. Did not realize that. Yup... learn something new every day. Okay then, I guess my comment is extraneous. 馃憤馃徏馃憤馃徏
Would you be able to provide an example of how such a method would be used? Presumably the motivation is to work around the shortcomings of GetValueOrDefault() when used over value types? If that is the case, would the already approved overloads in #20064 not suffice to work around this problem?
From giving it a quick look (didn't read fully in-depth), I think so. The problem I came here and added my vote for was the use case mentioned in that thread... of needing to return a default value from an Enumberable of enums, but not being able to use FirstOrDefault, because "was it a valid value (0) or was it empty, and so it returned default (0)."
@eiriktsarpalis The OrDefault methods approved in #20064 can't cover this use case because they do not allow lazy evaluation to obtain the default value.
With FirstOrNull:
var foo =
list.FirstOrNull(f => f.IsPrimary)
?? list.FirstOrNull()
?? SomeExpensiveOperation();
How would you implement the same thing with the proposal in #20064? Even if the OrDefault methods gained Func<T> defaultFactory methods, the syntax would involve unwieldly nesting (and avoidable allocations) rather than a linear pipeline.
Taking a look at how the Roslyn compiler codebase uses these extension methods might be useful.
@eiriktsarpalis
C#
var intArray = new[] { ... };
var firstLessThanOne = intArray.FirstOrNull((i) => i < 1);
With FirstOrDefault, you can't distinguish between there is zero existing in the list, or there is no number less than one
With 20064, you don't have ability to specify null as default for struct, or you need to cast number to be nullable
@jnm2 Noted. With the upcoming inclusion of discriminated unions in future versions of C#, I wouldn't be too surprised if we ended up adding a true option type. This would provide a solution that works for all types and not just structs.
As long as the syntax example above wouldn't become belabored, I like that concept a lot. The solution for non-nullable reference types in that example is of course to swap FirstOrNull with FirstOrDefault, but there is no nice solution for nullable reference types without an option type.
Most helpful comment
FirstOrNull makes much more sense. 'Nullable' includes both
nulland values.I have
*OrNullextension methods that travel around with me for pretty much every*OrDefaultBCL API, e.g.GetValueOrNull(TKey key)on IReadOnlyDictionary.