Reputation: 2109
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?
Upvotes: 28
Views: 74974
Reputation: 2525
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
Upvotes: 2
Reputation: 16257
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.
Upvotes: 3
Reputation: 107327
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))
...
Upvotes: 2
Reputation: 7871
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.
Upvotes: 1
Reputation: 838796
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.
Upvotes: 40
Reputation: 17804
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"
Upvotes: 4