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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T16:06:25+00:00 2026-05-11T16:06:25+00:00

I have a question on programming style and C# language design in general, I’d

  • 0

I have a question on programming style and C# language design in general, I’d love to know if there is a better way to do what I’m doing.

If you have a complex data object, with properties that can be null but you want to check or operate on data if it is there, you cannot write a line like so

if(Myobject.MyNestedObject != null || Myobject.MyNestedObject.Property != null)
{
   //code
}

Because the compiler will actually call both lines of code to evaluate the if statement.

Instead you must (I believe) write :

if(Myobject.MyNestedObject != null)
{
   if(Myobject.MyNestedObject.Property != null)
   {
      //code
   }
}

Is there a better style than this? I’m trying to think of how to use null coalesce (??) but it would still throw if you try to use anything of MyNestedObject in the same statement.

More info:

    L_01b4: ldarg.1 
    L_01b5: callvirt instance class [Myassembly]MyClass.MyObject [MyAssembly]MyClass::get_MyObject()
    L_01ba: brtrue.s L_01cc
    L_01bc: ldarg.1 
    L_01bd: callvirt instance class [MyAssembly]MyClass.MyObject [MyAssembly]MyClass::get_MyObject()
    L_01c2: callvirt instance class [MyAssembly]MyClass.MyNestedObject [MyAssembly]MyClass.MyNestedObject::get_MyNestedObject()
    L_01c7: ldnull 
    L_01c8: ceq 
    L_01ca: br.s L_01cd
    L_01cc: ldc.i4.0 
    L_01cd: stloc.2 
    L_01ce: ldloc.2 
    L_01cf: brtrue L_0285
    L_01d4: nop 

From my understanding it’s saying that at L_01ba if the call returns true, not null or non-0 (i.e if the object is null, the branch isn’t taken and then control flow continues linearly). This then will of course execute L_01c2 which will throw a null reference exception, as Myclass.MyObject is null.

Have I missed something. This is the .net 3.5 C# compiler.

  • 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-11T16:06:25+00:00Added an answer on May 11, 2026 at 4:06 pm

    Combining @Chris and @aJ answer:

    I think you want the && operator, not ||.

    if (Myobject.MyNestedObject != null &&
        Myobject.MyNestedObject.Property != null)
    {
        //code
    }
    

    And C#’s && operator use short-circuit evaluation, so if the first expression returns false, the second expression will not be evaluated.

    …

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

Sidebar

Ask A Question

Stats

  • Questions 85k
  • Answers 85k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer The contents of the file /etc/rc.local are executed on startup.… May 11, 2026 at 5:11 pm
  • Editorial Team
    Editorial Team added an answer There is already a Thread class in ruby maybe its… May 11, 2026 at 5:11 pm
  • Editorial Team
    Editorial Team added an answer You can try http://www.cobolproducts.com/datafile/data-viewer.html You might atleast get an idea… May 11, 2026 at 5:11 pm

Related Questions

I've been thinking lately, would it be a good form of syntactic sugar in
I'm preparing a class on Visual Basic 2005 targeting Visual Basic 6 programmers migrating
Where to read up on (the best-practice in) source code formatting, for individual languages
I just lost 50% of my answer on a test because I wrote the
I've always been one to err on the side of preventing exception conditions by

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.