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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T16:00:12+00:00 2026-05-21T16:00:12+00:00

I have this code: MyClass _localMyClass = MyClassDAO.GetMyClassByID(123) ?? new MyClass(); This is the

  • 0

I have this code:

MyClass _localMyClass = MyClassDAO.GetMyClassByID(123) ?? new MyClass();

This is the method:

    public static MyClass GetMyClassByID(int id)
    {
        var query = from m in ctx.MyClass
                    where m.MyClassID == id
                    select m;

        return query.FirstOrDefault<MyClass>();
    }

So the FirstOrDefault() doesn’t find a hit in the entity framework context, which is the “ctx” object. So the ‘default’ value to be returned is null, since the target of the query is a class.

The result of the first bit of code, using the ??, results in _localMyClass being what? I would say it would be the new MyClass(). Instead, _localMyClass ends up being null. I tried grouping the logic with various sets of parentheses, but still no luck.

Odder still; when I set a debug break point, and copy/paste the MyClassDAO.GetMyClassByID(123) ?? new MyClass() into the watch screen of Visual Studio, the result is the new MyClass() instead of null.

Can anybody explain why it would be working in this manner? Why it doesn’t recognize the method return value as null and then instead use the new part?

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

    After closing and reopening Visual Studio, the code worked fine.

    I’m using Visual Studio Ultimate 2010 w/ service pack 1.

    I have had this happen in the past, and should have remembered to try it first. I have noticed that VS seems to behave strangely sometimes, especially when running the web application in debug, specifically when running the web app locally (from my local IIS) and attaching to w3wp.exe.

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

Sidebar

Related Questions

I have this function public static implicit operator MyClass(string v) { return new MyClass(v);
I have read a parent Class called MyClass code like this, public class MyClass
I have this following class: Class myclass{ public static $item_row_name = array( array('Item 1','Item_1','Help
I have this class: public class MyClass { [Column(Name=StoredColumn, DbType=int)] public int Stored; public
I have this code: template <class T> class MyClass { public: template <class U>
i have this scenario: class MyClass { Producer p; Consumer c; public static void
If I have code like this: string s = MyClass.GetString(); // Returns string containing
In my code, I have something that looks like this: @implementation MyClass - (id)
Hi, i'm have this code for initialize my class. - (id)initWithSize:(int)size { self =
I have this code #include <iostream> using namespace std; int main(int argc,char **argv) {

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.