✅ Nice way to get an item from a list only if there is one, otherwise return null?
Is there something like SingleOrDefault() that returns default instead of exception?
This throws an exception if more than 1, however I want 'default' if more than 1
47 Replies
Just
Angius
REPL Result: Failure
Exception: CompilationErrorException
Compile: 378.813ms | Execution: 0.000ms | React with ❌ to remove this embed.
Well, with a cast
might work, or
maybe
how does that ensure there is only 1?
is [var el]
Pattern matchinghmm ok not familar with that syntax, i'll look it up
Matches a list with only one element
I would just check the count but I'm old fashioned.
yeah just wondering if theres a "modern" way to do it
or also was considering copying the SingleOrDefault source and modify to return Default instead of throw exception
Angius
REPL Result: Success
Console Output
Compile: 493.418ms | Execution: 108.732ms | React with ❌ to remove this embed.
You can, of course, do it the old fashioned way
no that's not what im trying to acheive
"Return if there's only one element,
null
otherwise"
That's what it does
Ah, it's about the default?
If so, then instead of (int?)null
or (char?)null
use default(int)
or default(char)
or whatever else the type isno
see the example i gave
just like SingleOrDefault() behaviour, but return null/default instead of throw exception if there are more than 1
So, in other words,
If there is one and only one element, return that element
If there is zero or more than one elements, return
default
matching elements, not list.count
?
Which of my examples returns a count?
Do you mean like
list.Where(x => x == 'a').FirstOrDefault()
?
Can't remember if FirstOrDefault has a predicate overload or notIt does, yeah
Ah then i'd just put that lambda in FirstOrDefault and remove the Where
FirstOrDefault will return 'a' , not default
So you want
default
if there's a single element, and otherwise null
?correct
Good luck using it with reference types, then, since the default for them is
null
Make an extension method that counts how many items match the predicate, then if it's 1, return that element
Otherwise return default
But sure
T
is whatever you want it to belist.Count(x => x == 'a') == 1 ? list.First(x => x == 'a') : default
You could optimise it more but stillyep ok thanks carrot I'll do that. I thought perhaps there was a more succint way with modern c# that i wasnt aware of
Write an extension method
Something like that, yeah
from there, just replace throw with return default
I probably wouldn't be throwing on null arguments
Since we have NRTs and all that
But yeah
I'd add
where TSource : struct
constraint maybe
Unless you do expect this method to always return null
with reference typesoh just realised that was .net 4.8 source code, i'll see what the modern source is
thank you
can you F12 into it on Visual Studio? is there an option for that?
to see c# source
i can only get as far as that
the function definition/comments
not the actual code
Not sure if VS comes with a decompiler by default
Might need something like, dunno, DotPeek?
VS can decompile
When I F12 I get this:
Oh that's from SourceLink though
Not actually decompiled.
But it will decompile as well.
You need these options enabled:
awesome 👍👍👍 thank you
Unknown User•10mo ago
Message Not Public
Sign In & Join Server To View
Use the /close command to mark a forum thread as answered