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 179329
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T14:23:16+00:00 2026-05-11T14:23:16+00:00

Say for example that I create a Duck Duck myDuck = DuckFactory.CreateDuck(); In the

  • 0

Say for example that I create a Duck

Duck myDuck = DuckFactory.CreateDuck(); 

In the past I’ve always check to see if myDuck is null

if (myDuck == null) {   // Do stuff } 

I’ve recently looked at some code that check for null first.

if (null == myDuck) {    // Do stuff } 

It seems to me that these are the same, but are there any differences between these two? Is there any performance benefits to one over the other? Is there a recommended best practice for checking if an object is null?

  • 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. 2026-05-11T14:23:17+00:00Added an answer on May 11, 2026 at 2:23 pm

    The second stems from C/C++ (and elsewhere) where this is a valid construct

    if (myDuck = null) { } 

    That’s not valid in C# (unless your expression resolves to a boolean). For example, this is valid C#

    bool b = ... if (b = true) { } 

    Putting the constant first was defensive to stop accidental assignment when you wanted comparison. I’d say more people put the variable to the left but there is nothing wrong with putting the constant to the left either.

    Just pick one and be consistent about it.

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

Sidebar

Related Questions

Say for example you had a shell script that utilises inotifywait in the following
To use as an example: lets say that I have 2 EditText s and
For example lets say I have a JSON object that contains states and cities.
Say that I require a querystring; for example itemid. If that querystring is for
The issue Let's say that I have example.com, example.org and example.net. All of these
For example, Lets say that I have a gridview column which has important controls,
Say I have components that I need to iterate through, for example From and
Let's say this page www.example.com/mypage returns some html that I want to parse in
For example, let's say I have a Maven build file that has dependencies located
Say I have a class ObjectA (a view controller for example), that does this

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.