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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T08:14:14+00:00 2026-06-03T08:14:14+00:00

In our C# code, we already test if a variable is null before attempting

  • 0

In our C# code, we already test if a variable is null before attempting to access it.

if (myInt.HasValue) {
  var yourInt = myInt;

  // do something with yourInt
}

My question: is there a difference in using the nullable property as if it wasn’t after it has been tested versus the following?

if (myInt.HasValue) {
  var yourInt = myInt.Value; // see the difference?

  // do something with yourInt
}

Is this just a matter of preference or is there a distinct reason or performance impact for using .Value even after the nullable object has passed that test?

UPDATE

I expanded on my second example, we already test with HasValue, but there we use .Value to access the value.

UPDATE 2

I updated the examples to use vars because in our code we don’t actually use int types, sorry about the poor example. In our code, we actually just use the object inside an NHibernate Criteria query – Expression.Eq("thing", myInt) query.

This doesn’t throw a compilation error. I was trying to simplify the example to get to the root of my question without getting NHibernate involved. Sorry if this invalidates some of the answers. I was just trying to see if there is a hit on performance if we force another method to find the value versus explicitly calling .Value.

  • 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-03T08:14:16+00:00Added an answer on June 3, 2026 at 8:14 am

    is this just a matter of preference or is there a distinct reason

    int yourInt = myInt.Value;
    

    will throw if HasValue is false.

    Thus, if you don’t want to experience InvalidOperationExceptions, check HasValue first.

    Note that

    int yourInt = myInt;
    

    is not legal (compile-time failure) because there is no implicit conversion from int? to int (what if the value is null; there’s no reasonable value to cast the int? to). You can say:

    int yourInt = (int)myInt;
    

    Note that this will throw if myInt is null though, just like accessing Value.

    One last note, if you’re okay with accepting a default value, you can use the null coalescing operator and say:

    int yourInt = myInt ?? defaultValue;
    

    This is equivalent to:

    int yourInt;
    if(myInt.HasValue) {
        yourInt = myInt.Value;
    }
    else {
        yourInt = defaultValue;
    }
    

    This doesn’t throw a compilation error. I was trying to simplify the example to get to the root of my question without getting NHibernate involved. Sorry if this invalidates some of the answers. I was just trying to see if there is a hit on performance if we force another method to find the value versus explicitly calling .Value.

    No. Performance is totally irrelevant here, especially if there is a database involved. This is absolutely not going have any meaningful performance difference, and will certainly not be a bottleneck. Just write the clearest code. Frankly, I find .Value to be the clearest.

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

Sidebar

Related Questions

Our Java code (not the test code) reads files from the current directory, which
We want to start letting our users help us test our feature changes before
Our code-signing certificate expired recently, so I renewed it and just published our first
In our code we have quite a few cases of this pattern: class outerClass
For our code review process we open the details of a shelveset to see
In our code base there is a function which should return either an object
In our code I have the following, for now please ignore the //* bits;
Currently our code uses a for-loop for filling a buffer holding a Unicode string
I am currently porting our code from an alpha (Tru64) to an i386 processor
We've been profiling our code recently and we've come across a few annoying hotspots.

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.