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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T01:36:10+00:00 2026-06-01T01:36:10+00:00

I was working with ?? operator in C# 4 and found a interesting feature.

  • 0

I was working with ?? operator in C# 4 and found a interesting feature.

This line of code always assigns null to existingObject if it is already null regardless of the searchForObject() return value (searchForObject returns a not null object, in fact it is a linq statement not a function and if the following statement will be replaced with simple if construct then existingObject will be assigned a not null object):

    existingObject = extstingObject ?? searchForObject();

Could someone explain why?

Here is a link to MSDN, it says:

A nullable type can contain a value, or it can be undefined. The ??
operator defines the default value to be returned when a nullable type
is assigned to a non-nullable type. If you try to assign a nullable
value type to a non-nullable value type without using the ?? operator,
you will generate a compile-time error. If you use a cast, and the
nullable value type is currently undefined, an
InvalidOperationException exception will be thrown.

The part about assigning a nullable type to a non-nullable type is not what I expected.

The problem was deferred initialization of local variables in the debugger of Visual Studio 2010.

  • 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-01T01:36:11+00:00Added an answer on June 1, 2026 at 1:36 am

    Apart from what I said in this comment:

    There is also a chance you’re not actually looking at the same code
    that you’re executing. It happens sometimes in Visual Studio. Rebuild
    the project, try clearing the bin folder, put some Debug.WriteLines
    in to trace what’s really going on in your app (instead of relying on
    the IDE’s Watch), etc.

    another question comes to mind – is existingObject really null after the assignment? 🙂 How have you asserted that?

    Have a look at my question (and the answers):

    Surprising CLR / JIT? behaviour – deferred initialization of a local variable

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

Sidebar

Related Questions

Recently, I has been working with Drools component. I found a very interesting thing.
This error: error C2678: binary '=' : no operator found which takes a left-hand
Ok, I am working through a book and trying to learn C++ operator overloading.
In C++03, Boost's Foreach, using this interesting technique , can detect at run-time whether
From what I have read, I found that a built-in ternary operator does not
From working on this specific situation, it was news to me that the logic
I'm working on a feature which requires me to get the contents of a
Possible Duplicate: How do I fix this Perl code so that 1.1 + 2.2
We must face serious issue while prepraing my project and working on code in
This is pasted from a website, which presumably was working. I did some googling

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.