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

  • Home
  • SEARCH
  • 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 7927445
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T19:18:41+00:00 2026-06-03T19:18:41+00:00

Sometimes, a value must be checked for equality with a constant. In such cases,

  • 0

Sometimes, a value must be checked for equality with a constant. In such cases, I’ve always seen the code like this:

if (!string.IsNullOrEmpty(text))
{
    if (text == "Some text here")¹
    {
        // Do something here.
    }
}

In my case, I would rather write:

if ("Some text here".Equals(text))
{
    // Do something here.
}

After all, if text is null, Equals will return false, which is expected. The inversion of a constant and a variable feels strange, but is still understandable for a beginner, and avoids the NullReferenceException which would be thrown with text.Equals("Some text here").

Am I missing something?

Why all source code I’ve seen use the syntax from the first example, and never from the second one?


¹ In real code, it would rather be a constant or a readonly field. To shorten the examples, I put the strings inline.

  • 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-06-03T19:18:43+00:00Added an answer on June 3, 2026 at 7:18 pm

    In such cases, I’ve always seen the code like this:

    You’re right to think that’s odd and unnecessary, because it is. It’s a completely superfluous null or empty check. Frankly, I’d admonish code like that in a code review.

    if (text == "Some text here") {
        // Do something here.
    }
    

    is perfectly fine and that’s what I’d use.

    Am I missing something?

    No, you’re not missing anything.

    Why all source code I’ve seen use the syntax from the first example, and never from the second one?

    Because you’re looking for love in all the wrong places?

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

Sidebar

Related Questions

Sometimes I write very short assembly functions like function SeniorBit(Value: LongWord): Integer; asm OR
Beyond perplexed this time... The simplest possible line of code works sometimes, sometimes it
I wish to search a database table on a nullable column. Sometimes the value
When,i try to assign null value to my pointer,it sometimes crashes on that line.
sometimes there are sites that have some small information window thats always visible when
Sometimes I encounter cases where I have to attach a method to a delegate
Sometimes it is useful to get the type name from static invoke, see this
look at this table please table |id| |name| |order| i must get the rows,
If no return value is found when it must be found, I return an
In SQL one might sometimes write something like DELETE FROM table WHERE column IS

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.