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??
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??
??
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.
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;
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
_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
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
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;
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.
(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!