1
get { return _a.B; }

set
{
   if (( _a.B.Equals(value ?? 0) != true ))
   {
      _a.B= value ?? 0;
   }
}

I have coded the above code in c#. In the above code what does (value ?? 0) != true mean??

SWeko
  • 30,434
  • 10
  • 71
  • 106
user1172982
  • 15
  • 1
  • 4
  • 3
    exact duplicate of http://stackoverflow.com/questions/3815741/what-does-operator-means-in-c – Ravi Gadag Jan 27 '12 at 07:19
  • 1
    why have you written code using an operator you don't understand? – Antony Scott Jan 27 '12 at 07:22
  • possible duplicate of [What do two question marks together mean in C#?](http://stackoverflow.com/questions/446835/what-do-two-question-marks-together-mean-in-c) – vitaut Feb 24 '12 at 19:35

9 Answers9

3

?? is the null-coalescing operator.

Expanded, it means:

value == null ? 0 : value;

Here is the verbose version of your set accessor without the operator.

set
{
   // assumes that this is a nullable type (based on usage)
   int? coalescedValue = value;

   if( coalescedValue == null )
   {
       coalescedValue = 0;
   }

   if( ( _a.B.Equals( coalescedValue ) != true ) )
   {
      _a.B = coalescedValue;
   }
}

There are dozens of discussions on SO regarding the usage of this operator.

Community
  • 1
  • 1
Tim M.
  • 53,671
  • 14
  • 120
  • 163
2

It's the null-coalescing operator.

As other answers have pointed out, it evaluates the first operand. If that isn't null, then that's the overall value. Otherwise, it evaluates the second operand, and that's the overall value. Importantly though, neither operand is evaluated more than once, and the second operand is only evaluated if the first operand is null. Things are slightly more complicated that due to the potential conversions between types, but that's the general principle.

(At least, that's the theory. There's a bug in the current MS implementation which means the first operand may be evaluated more than once - but it shouldn't be.)

One other useful point to note is that the first operand type must be nullable, but the second doesn't have to be - and in the common case where the first operand is of type Nullable<T> and the second operand is type T, the type of the overall expression is T. So for example:

int defaultValue = 10;
int? potentialValue = GetValueFromSomewhere();
int valueOrDefault = potentialValue ?? defaultValue;
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
0

If value is null it returns what is after the ??, otherwise it returns the value it's self. http://msdn.microsoft.com/en-us/library/ms173224.aspx

Bradley Uffner
  • 16,641
  • 3
  • 39
  • 76
0
_a.B.Equals(value ?? 0) 

can be rewritten as

_a.B.Equals(value == null ? 0 : value) 

That means if _a.B is equal to value, if value is not null otherwise equal to zero

Maheep
  • 5,539
  • 3
  • 28
  • 47
0

a ?? b is basically shorthand for

(a != null) ? a : b

which means "give me a, but if a is null, give me the default value of b".
Further the ? : expression is shorthand for:

if (a != null)
   a
else
   b
SWeko
  • 30,434
  • 10
  • 71
  • 106
0

It means that if value == null 0 is returned, otherwise value is returned

Stig
  • 1,323
  • 16
  • 22
0

a = b??c could be seen to expand to:

if(b != Null) {
    a = b;
} else {
    a = c;
}

or, in another shorthand notation [using the ternary operator]:

a = (b!=Null)?b:c;
Rohan Prabhu
  • 7,180
  • 5
  • 37
  • 71
0

The ?? operator is called the null-coalescing operator and is used to define a default value for nullable value types or reference types. It returns the left-hand operand if the operand is not null; otherwise it returns the right operand.

Madhu Beela
  • 2,205
  • 16
  • 16
0

(value ?? 0) != true does not mean anything, it's _a.B.Equals(value ?? 0) != true that means something.
And that something is !(_a.B == (value ?? 0)), that is the opposite of "is _a.B equals to value if value is not null or equals to 0 if _a.B is null).

You can rewrite the code snippet this way:

set
{
    if (value == null)
    {
        if (_a.B != 0)
        {
            _a.B = 0;
        }
    }
    else
    {
        if (_a.B != value)
        {
            _a.B = value;
        }
    }
}

This is a lot longer, but far more readable!

Falanwe
  • 4,636
  • 22
  • 37