-
Notifications
You must be signed in to change notification settings - Fork 4.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
FirstOrNullable and similar method for struct collection #28650
Comments
I assume that this would be equivalent to I think that |
@Grauenwolf Not sure but I think implementation is even easier than that public static T? FirstOrNullable<T>(this IEnumerable<T> items) where T : struct
{
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 I have |
@Grauenwolf no it would be faster, |
@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 |
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 |
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 Taking a look at how the Roslyn compiler codebase uses these extension methods might be useful. |
var intArray = new[] { ... };
var firstLessThanOne = intArray.FirstOrNull((i) => i < 1); With |
@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 |
I just stumbled upon a use case for this: Current alternatives: // besides having to cast everything to Nullable, I have to use ! and .Value inside the predicate, unnecessarily... :(
var uf = addressComponents.EnumerateArray().Cast<JsonElement?>()
.FirstOrDefault(a => a!.Value.GetProperty("types").EnumerateArray().Select(t => t.GetString())
.Contains(address_type_1))?
.GetProperty("short_name").GetString();
// OR
// without the cast to JsonElement?, I'm unable to use the safe navigation operator
var addressType1Value = addressComponents.EnumerateArray()
.FirstOrDefault(a => a!.Value.GetProperty("types").EnumerateArray().Select(t => t.GetString())
.Contains(address_type_1));
string uf = null;
if (addressType1Value.ValueKind != JsonValueKind.Undefined)
{
uf = addressType1Value.GetProperty("short_name").GetString();
} With the proposed API: // no unnecessary casting, no ! and .Value clutter
var uf = addressComponents.EnumerateArray()
.FirstOrNull(a => a.GetProperty("types").EnumerateArray().Select(t => t.GetString())
.Contains(address_type_1))?
.GetProperty("short_name").GetString(); |
@eiriktsarpalis Completed? Which version? |
I have no idea how this happened. This hasn't been addressed obviously. |
Please add these to
System.Linq.Enumerable
The text was updated successfully, but these errors were encountered: