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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T05:17:52+00:00 2026-05-30T05:17:52+00:00

We just ran across some bad code like this in our c#.net 4 codebase

  • 0

We just ran across some bad code like this in our c#.net 4 codebase

DateTime myDate = someValue;
If (myDate==Null)
    Do Something

It occurred to us that this condition will never occur.

How does the compiler handle these non-nullable struct comparisons?

Originally we were surprised that it would compile… but rationalized it on the point that you could certainly have a constant comparison like:

If(1==2)

Which would also never resolve true… but in that case the compiler can easily tell they are constants. Does it optimize or rollup non-nullable comparisons?

  • 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-30T05:17:54+00:00Added an answer on May 30, 2026 at 5:17 am

    I punched this into LinqPad:

    var t = new DateTime();
    t.Dump();
    (t == null).Dump();
    

    And got this:

    IL_0000:  ldloca.s    00 
    IL_0002:  initobj     System.DateTime
    IL_0008:  ldloc.0     
    IL_0009:  call        LINQPad.Extensions.Dump
    IL_000E:  pop         
    IL_000F:  ldc.i4.0    
    IL_0010:  call        LINQPad.Extensions.Dump
    

    So yes, the compiler compiles it to the same as:

    var t = new DateTime();
    t.Dump();
    (false).Dump();
    

    Interestingly, if I create my own struct (TestStruct) and try this:

    TestStruct t;
    (t == null).Dump();
    

    … the compiler complains that I can’t do an equals comparison between TestSruct and null.

    Update

    In a comment, Paolo points to another StackOverflow post reporting this last phenomenon. Apparently by overloading the == and != operators, a value type becomes subject to an automatic conversion from t == null to (Nullable<TestClass>)t == (Nullable<TestClass>)null. If you haven’t overloaded those operators, this implicit conversion doesn’t make sense, so you get an error.

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

Sidebar

Related Questions

I just ran across some jQuery that looks like this: $('.add-row').live('click.add', function() { //
I just ran across some code while working with System.DirectoryServices.AccountManagement public DateTime? LastLogon {
I'm looking at some GXT code for GWT and I ran across this use
I'm just starting out with iphone development and ran across some example code that
I just ran across this error message while working in C# A property or
I just ran into some code that overuse semicolons, or use semicolon for different
Some old code that I just came across: MLIST * new_mlist_link() { MLIST *new_link
I just ran across the following syntax in a piece of Haskell code -
Though I'm of course familiar with auto-properties, I just ran across this at work,
The code golf series seem to be fairly popular. I ran across some code

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.