Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1808852
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T06:19:08+00:00 2026-05-17T06:19:08+00:00

I often do this when necessary to prevent a null pointer exception : //

  • 0

I often do this when necessary to prevent a null pointer exception:

// Example #1
if (cats != null && cats.Count > 0)
{
  // Do something
}

In #1, I’ve always just assumed that the cats != null needs to be first, because order of operations evaluate from left to right.

However, unlike example #1, now I want to do something if the object is null or if the Count is zero, therefore I’m using logical OR instead of AND:

// Example #2
if (table == null || table.Rows == null || table.Rows.Count <= 0)
{
  // Do something
}

Does the order of the logical comparisons matter? Or can I also reverse the order and get the same results, such as in Example #3?

// Example #3
if (table.Rows.Count <= 0 || table.Rows == null || table == null)
{
  // Do something
}

(btw, I realize I can rewrite #2 like below, but I think it’s messy, and I’m still curious about the OR operators)

// Example #4
if (!(table != null && table.Rows != null && table.Rows.Count > 0))
{
  // Do something
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T06:19:08+00:00Added an answer on May 17, 2026 at 6:19 am

    Yes, the short-circuiting happens in both cases, the only difference being that && stops if the LHS is false (because the overall expression must then be false) while || stops if the LHS is true (because the overall expression must be true).

    The first two examples in your question are correct, the third will throw an exception if table or table.Rows are null.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

When I'm writing code with QtCreator (2.4.1), I'm very often in this situation: something
Quite often in my GUI code, I write something like this: private void SecondTimer_Elapsed(object
This often happens to me: I'm working on a couple related changes at the
I often do this: git add -u git commit ...discover mistake, fix it... git
my app crashes often in this for-loop: for (int a = 0; a <=
i've often had this issue where i do not really understand how to pass
In classes that implement INotifyPropertyChanged I often see this pattern : public string FirstName
Looking back at my past projects I often encounter this one: A client or
In answering this question , it got me thinking... I often use this pattern:
I didn't take any official course of Javascript. I often read this in tutorials:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.