So I've got a collection of struct
s (it's actually a WCF datacontract but I'm presuming this has no bearing here).
List<OptionalExtra> OptionalExtras;
OptionalExtra
is a struct
.
public partial struct OptionalExtra
Now I'm running the below statement:
OptionalExtra multiOptExtra = OptionalExtras.Where(w => w.Code == optExtra.Code).FirstOrDefault();
if (multiOptExtra != null)
{
}
Now this won't compile:
the operator != cannot be applied to opperands of type OptionalExtra and
'<null>'
After a little googling I realised it's because OptionalExtra
is a struct
. Which I believe is not nullable unless defined as a nullable type?
So my question is, if my where
statement returns no results what will be the outcome of the FirstOrDefault
call? Will it thrown an exception?
Incidently this should never happen but better safe than sorry.
question from:https://stackoverflow.com/questions/15407065/firstordefault-result-of-a-struct-collection