Working on a project and the coder does this a lot in his checks. First, he checks if the nullable int
has a value, and then he checks if it's greater than 0. Why? Why make two checks if one check (if it is greater than 0) should be sufficient? Since nulls are not greater than 0, wouldn't that be redundant?
The code is probably redundant.
If i is int?
then:
if (i.HasValue && i.Value > 0)
is equivalent to:
if (i > 0)
From MSDN:
When you perform comparisons with nullable types, if the value of one of the nullable types is null and the other is not, all comparisons evaluate to false except for != (not equal). It is important not to assume that because a particular comparison returns false, the opposite case returns true. In the following example, 10 is not greater than, less than, nor equal to null. Only num1 != num2 evaluates to true.
i > 0
would be i <= 0
. For an int?
that's not true (both expressions will return false). Explicitly checking for i.HasValue
points that out. Also, i != 0
will return true, even though i
doesn't actually have a value. –
Dropout if (i is > 0)
–
Comehither It might be that the value for the variable has different meanings in that context.
int? someNumber = null; //might mean "there is no value"
int? someOtherNumber = 0; //might mean "the user has selected: 0"
The following:
class Program {
static void Main(string[] args) {
int? i = null;
if (i > 0) { Console.WriteLine(">0");
} else { Console.WriteLine("not >0");
}
if (i < 0) { Console.WriteLine("<0");
} else { Console.WriteLine("not <0");
}
if (i == 0) {Console.WriteLine("==0");
} else { Console.WriteLine("not ==0");
}
Console.ReadKey();
}
}
will output
not >0
not <0
not ==0
without throwing an exception. So the null/HasValue check in this case is redundant. There is one small difference. The following:
(i.HasValue && (i.Value == 0))
is about twice as fast as
(i == 0)
when i is null although both are so fast it's not an important difference. When i has a value, the two comparisons take about the same amount of time.
Since by default an int
cannot be null
and its value will be set to 0
, the operator of >
and <
expects to work with values
and not with nulls
, so that's why you must first check if it's null
, because it will cause an error if it is null.
You can use a logic that will always return an int
, even if it's null
it will 'not' return null
but it will return 0
which is the default value, and by using this you can check for null
+ >
at once.
Here are some ways to do it:
int? nullableNum = null;
int number;
number = nullableNum.GetValueOrDefault(); // will return 0 if null
number = nullableNum ?? 0; // will return 0, or whatever you insert
If you don't know if the nullableNum
will be a nullable type
or not (usually not relevant in C#), then in case it turns out not to be nullable
, the null
operator won't work and nor the GetValueOrDefault()
method, so in that case you can cast its type
to a nullable int
and then check:
number = ((int?)nullableNum) ?? 0
You might find that the programmer is used to the following type of checking on reference types before dereferencing them. Given that the the Nullable HasValue
is similar in concept to the null check, I guess the pattern 'stuck', even though it is redundant with nullable types.
if ((myObject != null) && (myObject.SomeValue > 0))
...
Null checking is generally done to prevent exceptions, or to set default values (prior to .NET 4). Checking for zero would be more of a business logic choice, depending on the circumstances.
© 2022 - 2024 — McMap. All rights reserved.
int? foo; if (foo.HasValue && foo > 0) ...
– Heinrikif (i.HasValue && i.Value > 0)
then he is doing three checks. First to see ifi
has a value. Second,i.Value
checks again to see ifi
has a value, because if it does not then it must throw an exception. And then finally the value is compared to zero. This is unlikely to be a performance bottleneck in your code, but still, it is good to know exactly what the code you are writing is doing. – Gigigigliif (i.GetValueOrDefault() > 0)
-- work out the cases. Ifi
is null then the default is zero, which is not greater than zero, so this will be false. ifi
is not null then we compare the value to zero. Now, I am not suggesting that you do this; writingif (i>0)
is considerably more clear, and the performance difference will be measured in nanoseconds. I'm just saying that it is smart to know precisely what your tools do. – Gigigigli