5

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?

8
  • 4
    Alternatively, you can avoid the use of operator?? by modifying your return statement to be return query.DefaultIfEmpty(new MyClass()).First(); Commented Apr 21, 2011 at 17:52
  • 1
    Try breaking it up into two lines (MyClass _localMyClass = MyClassDAO.GetMyClassByID(123); _localMyClass = _localMyClass ?? new MyClass();) and stepping through it. Commented Apr 21, 2011 at 17:53
  • Can you show the entire class? I'd like to see where the top line is in relation to the rest of it. Commented Apr 21, 2011 at 17:54
  • Wow... I'm a bit cranky now... I closed and reopened Visual Studio 2010... and it works fine now. Nice. Commented Apr 21, 2011 at 18:04
  • Sorry for the confusion. And I appreciate all the suggestions VERY MUCH! I can't 'answer my own question' for 8 hours. Commented Apr 21, 2011 at 18:05

3 Answers 3

2

Just a guess because I made this mistake before and it baffled me for the longest time.

Do you have a conditional breakpoint and in that conditional breakpoint do you have the condition as _localMyClass = null?

One time I did something like that when I meant to write == in the conditional breakpoint and it resulted in the debugger setting the value to null. Similarly do you have any watch values that could be setting it to null?

Sign up to request clarification or add additional context in comments.

Comments

1

Ok, since the self-answer has already been accepted, a pro-forma answer in case anyone needs another possible case in the future:

 FirstOrDefault() 

will in fact return the default value which can be null for object references, but cannot be that for value-types. If MyClass is a valuetype (think: struct) you'd be better of

  • checking for default(T) in a generic method
  • using a specific value using DefaultIfEmpty() extension
  • using the MyClass? nullable type (short for Nullable)

$0.02

1 Comment

Excellent point on the value-type. Always be sure you know what it is that you are running a query against and what the return value should be. In my example, if somebody modified the query to return a field in MyClass, instead of the entire MyClass, then odds are that field would be a value-type (int, bool, etc.), which would then indeed fail the ?? check since value types can't be null. The firstOrDefault() would instead return a 0 for int fields, false for bools, etc. If you are not sure what FirstOrDefault() will return, ALWAYS double check!
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.