# How do i handle .FirstOrDefault method?

When i am querying database with `.FirstOrDefault` method, how do i handle results that it has given to me? I am especially concerned about null values, currently i have something like that:

``````if (result == default(TypeOfResult))
{
handleIt();
}
``````

But i do not exactly know what is this "default", so i am wondering if it wasn't better to do it this way:

``````if (result == null)
{
handleIt();
}
``````

which one would work? And what exactly is this "default"?

## 4 Answers

`FirstOrDefault` will return the first element in the sequence or literally the default value for the type in question.

So depending on what you're querying the default value may change. For example, a collection of `int`'s the `default` value will be 0. So, checking if `null` would not work.

Consider:

``````List<int> s = new List<int>();
var x = s.FirstOrDefault();
``````

Here `x` would equal `0`

How about a reference type?

``````List<MyCustomClass> s = new List<MyCustomClass>();
var x = s.FirstOrDefault();
``````

Here `x` would be `null`

This is probably the better of the two approaches:

``````if (result == default(TypeOfResult))
{
handleIt();
}
``````

If `TypeOfResult` is a reference type then

`result == default(TypeOfResult)` and `result == null` means the same thing.

Because the default value for reference types in `null`.

The default keyword just returns a "default value" for a given type which in case of reference types in `null`. For value types it depends on the type e.g. 0 for `int` etc.

And as the name implies `FirstOrDefault` will return the first element of the collection or the default value for the given type if the collection is empty.

If you want do ensure that your code can work failsave with default(T) where T is a value type you can box your items in the Nullable object.

For example:

``````var ints = new List<int>(){1,2,3,4,6};
int result = ints.Where(i => i == 0).FirstOrDefault();
``````

result is 0 even if there is no 0 in the list!

``````var ints = new List<int>(){1,2,3,4,6};
var nullable = ints.Select(i => new Nullable<int>(i));
var result = nullable.Where(i => i == 0).FirstOrDefault()
``````

result is null, there is no 0 in the list!

This is of course slower and requires more memory, but also works for non-value types.

You should check with Null. FirstOrDefault will return NULL if cannot find any result.

See this POST
{"ft_lang_label":"__label__en","ft_lang_prob":0.901599,"math_prob":0.90696144,"size":442,"snap":"2019-13-2019-22","text_gpt3_token_len":103,"char_repetition_ratio":0.11415525,"word_repetition_ratio":0.0,"special_character_ratio":0.25339368,"punctuation_ratio":0.12941177,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.95975864,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2019-05-22T13:50:52Z\",\"WARC-Record-ID\":\"<urn:uuid:f090bc0f-6dd2-4a92-9190-1b991473daa3>\",\"Content-Length\":\"136375\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:b1a49ca6-01e4-43fc-85cf-a4ad0d442b12>\",\"WARC-Concurrent-To\":\"<urn:uuid:680d7861-d076-4f61-a990-ab418fc831d2>\",\"WARC-IP-Address\":\"151.101.65.69\",\"WARC-Target-URI\":\"https://stackoverflow.com/questions/12095649/how-do-i-handle-firstordefault-method\",\"WARC-Payload-Digest\":\"sha1:GAY6ZRLVTKTKPXRQTVFLLZKF3PBJ4BTK\",\"WARC-Block-Digest\":\"sha1:LVVG3O6RDR5DIVBKL5AFILDLSUN3CXRA\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2019/CC-MAIN-2019-22/CC-MAIN-2019-22_segments_1558232256812.65_warc_CC-MAIN-20190522123236-20190522145236-00473.warc.gz\"}"}