3 Replies - 214 Views - Last Post: 12 February 2019 - 08:40 AM Rate Topic: -----

#1 bluemarmalade   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 05-July 15

How to check for null or empty in controllers?

Posted 12 February 2019 - 02:11 AM

I'm wondering what is the right way to check for null or empty entries in controllers in asp.net core and c# in general.

There are so many instances where I have to check this for various scenarios:

From database calls:
When I use entityframework core, I often use FirstOrDefault() when querying a table. This returns a default value if none is found. but what is this value and how do I check if this default value is returned?
var person = await _context.Persons.FirstOrDefaultAsync(i => i.ID == id);
//what is the difference here:
if(person is null)
...
if(person == null)
...



How about if I return a collection?
var persons = _context.Person.ToList();
if(persons.Any())
...
if(persons is null)
...
if(persons == null)
//you can probably also use Count() too



Then you have strings:
//model.Name is a string returned from frontend
if(string.IsNullOrWhiteSpace(model.Name))
...
if(model.Name is null)
if(model.Name == null)



I see no consistency on this when I read other code and I have really not much clue myself. But I want to find out what is the best way to check for each scenario. I would imagine there are articles on this?

Is This A Good Question/Topic? 0
  • +

Replies To: How to check for null or empty in controllers?

#2 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 14987
  • View blog
  • Posts: 59,833
  • Joined: 12-June 08

Re: How to check for null or empty in controllers?

Posted 12 February 2019 - 07:59 AM

Is and == are different beasts.

https://docs.microso...nce/keywords/is
https://docs.microso...arison-operator

Are you doing different things if it is an 'is' or a '=='?

Have you looked into null conditional operators or null coalescing operators?

https://docs.microso...ional-operators
https://docs.microso...escing-operator
Was This Post Helpful? 0
  • +
  • -

#3 andrewsw   User is online

  • quantum multiprover
  • member icon

Reputation: 6791
  • View blog
  • Posts: 28,045
  • Joined: 12-December 12

Re: How to check for null or empty in controllers?

Posted 12 February 2019 - 08:30 AM

FirstOrDefault would return the default, if no item were found, according to the object-type. For an integer, a value-type, the default is 0, for a boolean it is false. For any other reference type (object) the default is null. A string is a reference-type so its default is also null.
Was This Post Helpful? 0
  • +
  • -

#4 andrewsw   User is online

  • quantum multiprover
  • member icon

Reputation: 6791
  • View blog
  • Posts: 28,045
  • Joined: 12-December 12

Re: How to check for null or empty in controllers?

Posted 12 February 2019 - 08:40 AM

If a list is declared, but not initialised, it is null,

List<Stuff> foo;

it has no value.

Once it has been set to something,

foo = new List<Stuff>();

it is no longer null. It is a list without any items. Any() or Count will confirm this.

Null and empty checks are a pain that we have to get used to. Reconciling between database NULL, DbNull, etc., is a further pain.

There is no universal cure. Practice and experience will lessen the pain.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1