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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T20:03:44+00:00 2026-05-25T20:03:44+00:00

Possible Duplicate: Which is preferred: Nullable<>.HasValue or Nullable<> == null? I’m working in a

  • 0

Possible Duplicate:
Which is preferred: Nullable<>.HasValue or Nullable<> == null?

I’m working in a codebase which uses both of the following forms for “safely” getting values out of Nullable types. For example, if foo is a Nullable (int?):

if (foo != null) {
    value = (int)foo;
}

if (foo.HasValue) {
    value = foo.Value;
}

I prefer the second form, but is there any particular context which might make the first (or the second, for that matter) certainly preferred over the other?

  • 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-25T20:03:45+00:00Added an answer on May 25, 2026 at 8:03 pm

    The first and second are exactly equivalent, and compile down to the same IL.

    EDIT: They do indeed generate the same IL, like this:

      IL_000b:  ldloca.s   V_1
      IL_000d:  call       instance bool valuetype 
                               [mscorlib]System.Nullable`1<int32>::get_HasValue()
      IL_0012:  ldc.i4.0
      IL_0013:  ceq
      IL_0015:  stloc.2
      IL_0016:  ldloc.2
      IL_0017:  brtrue.s   IL_0023
      IL_0019:  nop
      IL_001a:  ldloca.s   V_1
      IL_001c:  call       instance !0 valuetype
                               [mscorlib]System.Nullable`1<int32>::get_Value()
      IL_0021:  stloc.0
    

    This is guaranteed by section 7.10.9 of the C# 4 spec (or the equivalent in other versions).

    Basically – use whichever form you and your team find more readable.

    Anton highlighted the null-coalescing operator – while Anton’s code isn’t equivalent to yours, it’s an operator that you definitely should be familiar with, as it can really make for nice code.

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

Sidebar

Related Questions

Possible Duplicate: C#: Passing null to overloaded method - which method is called? Here
Possible Duplicate: Which is faster/best? SELECT * or SELECT column1, colum2, column3, etc What
Possible Duplicate: Which collection for storing unique strings? I am currently using a Dictionary<string,
Possible Duplicate: Which is faster/best? SELECT * or SELECT column1, colum2, column3, etc. I
Possible Duplicate: How do I find out which DOM element has the focus? Is
Possible Duplicate: NAnt or MSBuild, which one to choose and when? What is the
Possible Duplicate: Casting vs using the ‘as’ keyword in the CLR Which method is
Possible Duplicate: Which sorting algorithm is used by STL’s list::sort()? Which sorting algorithm can
Possible Duplicate: Which IDE for Scala 2.8? I'm learning Scala by reading 'Programming in
Possible Duplicate: Close and Dispose - which to call? Hi, After reading some web

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.