BanditoBunny
BanditoBunny

Reputation: 3896

How does the C# operator `??` works?

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.

Upvotes: 2

Views: 7347

Answers (6)

Pranay Rana
Pranay Rana

Reputation: 176896

Read : Coalescing operator - ?? : Coalescing operator is new operator added in C#2.0. Coalescing operator is also known as ??.

Without ?? operator

 Nullable<int> a = null; 
Nullable<int> b = 10; 
int c = a==null ? b.Value : a; 

Can be replace with ?? like as below

Nullable<int> a = null; 
Nullable<int> b = 10; 
int c = a ?? b.Value; 

?? is oeprate allows to assign second value if the first one is null

existingObject = extstingObject ?? searchForObject(); 

so in your case if extstingObject is null tha its get the value of searchForObject() and assign to object , if you are getting null than the second function also return null also.

Upvotes: 3

Filip Ekberg
Filip Ekberg

Reputation: 36287

There is something else going on here, searchForObject() must return null. Take a look at the following example.

object searchForObject()
{
    return new object();
}

With this implementation of searchForObject() we can do this:

object existingObject = null;
existingObject = existingObject ?? searchForObject();

Now if we test this out simply like this:

Console.Write("existingObject is ");
if (existingObject == null) Console.WriteLine("null");
else Console.WriteLine("not null");

It is going to tell us that existingObject is not null. However, if we change what searchForObject() returns to this:

object searchForObject()
{
    return null;
}

It is going to tell us that existingObject is null. Now, a last test is to change the value of existingObject before we do the first check:

object existingObject = new object();
existingObject = existingObject ?? searchForObject();

This will tell us that existingObject is not null.

If existingObject is null, this means that whatever is after ?? returns null.

Upvotes: 1

Konrad Morawski
Konrad Morawski

Reputation: 8394

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

Upvotes: 1

Habib
Habib

Reputation: 223257

?? returns the left-hand operand if the operand is not null; otherwise it returns the right operand.

   // ?? operator example.
    int? x = null;
    // y = x, unless x is null, in which case y = -1.
    int y = x ?? -1;

Upvotes: 3

Cheng Chen
Cheng Chen

Reputation: 43523

The reason is somewhere else is wrong. In this line of code, the return value of searchForObject() will be assigned to existingObject if existingObject is null.

Upvotes: 0

Jon
Jon

Reputation: 40032

The ??is shorthand for

if (somethingisnull)
  somethingisnull = somethingnotnull

Therefore searchForObject() must be returning null as well

Upvotes: 4

Related Questions