Problem
Nullable> is something I’ve always used. I chose HasValue because of the semantics. However, I was recently working on someone else’s existing software, and they primarily utilized Nullable>!= null.
Is there a reason to prefer one over the other, or is it simply a matter of personal preference?
vs.
Asked by lc.
Solution #1
There is no significant change because the compiler replaces null comparisons with a call to HasValue. Simply choose the option that is more readable and makes more sense to you and your colleagues.
Answered by Rex M
Solution #2
So that the syntax matches reference types, I prefer (a!= null).
Answered by cbp
Solution #3
I conducted some testing on this by assigning values to a nullable int using various ways. Here’s what occurred when I tried a few different things. It’s necessary to clarify what’s going on. Keep in mind the following: Is it a nullable or a shortcut for something? is a struct for which the compiler appears to have gone to great lengths to allow us to utilize null as if it were a class. SomeNullable == null and SomeNullable.HasValue will always return the expected true or false, as shown below. SomeNullable == 3 is also legal (assuming SomeNullable is an int? ), albeit it isn’t shown here. If we assign null to SomeNullable.Value, however, we get a runtime error. Because of this, nullables are the only instance where they potentially give us problems.
Here’s a rundown of some code I used, along with the labels it generated:
int? val = null;
lbl_Val.Text = val.ToString(); //Produced an empty string.
lbl_ValVal.Text = val.Value.ToString(); //Produced a runtime error. ("Nullable object must have a value.")
lbl_ValEqNull.Text = (val == null).ToString(); //Produced "True" (without the quotes)
lbl_ValNEqNull.Text = (val != null).ToString(); //Produced "False"
lbl_ValHasVal.Text = val.HasValue.ToString(); //Produced "False"
lbl_NValHasVal.Text = (!(val.HasValue)).ToString(); //Produced "True"
lbl_ValValEqNull.Text = (val.Value == null).ToString(); //Produced a runtime error. ("Nullable object must have a value.")
lbl_ValValNEqNull.Text = (val.Value != null).ToString(); //Produced a runtime error. ("Nullable object must have a value.")
Let’s move on to the next technique of initialization:
int? val = new int?();
lbl_Val.Text = val.ToString(); //Produced an empty string.
lbl_ValVal.Text = val.Value.ToString(); //Produced a runtime error. ("Nullable object must have a value.")
lbl_ValEqNull.Text = (val == null).ToString(); //Produced "True" (without the quotes)
lbl_ValNEqNull.Text = (val != null).ToString(); //Produced "False"
lbl_ValHasVal.Text = val.HasValue.ToString(); //Produced "False"
lbl_NValHasVal.Text = (!(val.HasValue)).ToString(); //Produced "True"
lbl_ValValEqNull.Text = (val.Value == null).ToString(); //Produced a runtime error. ("Nullable object must have a value.")
lbl_ValValNEqNull.Text = (val.Value != null).ToString(); //Produced a runtime error. ("Nullable object must have a value.")
Everything is the same as before. Remember that using int? val = new int?(null); with null as the constructor parameter would result in a COMPILE time error since the nullable object’s VALUE is NOT nullable. Only the wrapper object itself has the ability to equal null.
Similarly, we’d get a compile time error if:
int? val = new int?();
val.Value = null;
not to mention the fact that val.Value is a read-only property, which means we can’t even use:
val.Value = 3;
However, polymorphous overloaded implicit conversion operators allow us to perform the following:
val = 3;
There’s no need to be concerned about polysomnography or whatever it’s called, as long as it works well, right? 🙂
Answered by Perrin Larson
Solution #4
In Visual Basic.NET. When you can use “.HasValue,” don’t use “IsNot Nothing.” In one place, I replaced “IsNot Nothing” with “.HasValue” and addressed a “Operation could disrupt the runtime” Medium trust error. I’m not sure why, but something is going on in the compiler that I’m not sure about. I’m guessing that “!= null” in C# has the similar problem.
Answered by Carter Medlin
Solution #5
If you’re using linq and want to keep your code brief, I always recommend using!=null.
And here’s why:
Assume we have a class Foo that has a nullable double variable. SomeDouble
public class Foo
{
public double? SomeDouble;
//some other properties
}
If we wish to get all Foo with non-null SomeDouble values from a collection of Foo (assuming some foos in the collection are null as well), there are at least three ways to build our function (if we use C# 6):
public IEnumerable<Foo> GetNonNullFoosWithSomeDoubleValues(IEnumerable<Foo> foos)
{
return foos.Where(foo => foo?.SomeDouble != null);
return foos.Where(foo=>foo?.SomeDouble.HasValue); // compile time error
return foos.Where(foo=>foo?.SomeDouble.HasValue == true);
return foos.Where(foo=>foo != null && foo.SomeDouble.HasValue); //if we don't use C#6
}
And in this circumstance, I always recommend going for the shorter option.
Answered by yan yankelevich
Post is based on https://stackoverflow.com/questions/676078/what-is-the-difference-between-nullablet-hasvalue-or-nullablet-null