Differencebetween Nullable<T>.Hasvalue or Nullable<T> != Null

What is the difference between Nullable T .HasValue or Nullable T != null?

The compiler replaces null comparisons with a call to HasValue, so there is no real difference. Just do whichever is more readable/makes more sense to you and your colleagues.

whats the difference (if any) between nullable == null and nullable.hasvalue

There is no difference between these two .
it's just matter of convention .

Just pick one (which you like) and stick with it .

Is there any difference between myNullableLong.HasValue and myNullableLong != null?

It's just syntactic sugar. They will behave exactly the same way - the nullity test actually gets compiled into a call to HasValue anyway.

Sample:

public class Test
{
static void Main()
{
int? x = 0;
bool y = x.HasValue;
bool z = x != null;
}
}

IL:

.method private hidebysig static void  Main() cil managed
{
.entrypoint
// Code size 25 (0x19)
.maxstack 2
.locals init (valuetype [mscorlib]System.Nullable`1<int32> V_0)
IL_0000: ldloca.s V_0
IL_0002: ldc.i4.0
IL_0003: call instance void valuetype [mscorlib]System.Nullable`1<int32>::.ctor(!0)
IL_0008: ldloca.s V_0
IL_000a: call instance bool valuetype [mscorlib]System.Nullable`1<int32>::get_HasValue()
IL_000f: pop
IL_0010: ldloca.s V_0
IL_0012: call instance bool valuetype [mscorlib]System.Nullable`1<int32>::get_HasValue()
IL_0017: pop
IL_0018: ret
} // end of method Test::Main

What is the difference hasvalue operator check for null

I think you can use this

var anyDate = Date.GetValueOrDefault(DateTime.MinValue);

Since you want to check for null and in that case assign a default value, this roughly translates to the form

if(Date.HasValue)
return Date.Value;
return defaultvalue;

Difference between Nullable types not having a value and being Null in C#

Nullable<T> is a struct, and being a struct, it cannot ever actually be null. The only actual data stored in a Nullable<T> is an underlying value of type T and a boolean HasValue flag. A Nullable<T> value in which HasValue is false can be thought of as being null, even though it technically is not null.

There is some compiler magic such that someNullable == null can be called, and the result of that expression is actually the same as someNullable.HasValue. You can also assign null to a Nullable<T>. What this will actually do is create a new Nullalbe<T> struct where HasValue is false. (Interestingly, it's not possible for you to do that yourself if you were to write your own type.)

There is some more compiler magic such that if you box a Nullalbe<T> it doesn't actually box the nullable type. If HasValue is actually true then it will pull out the underlying value and box that. If HasValue is false then it will just box null. When unboxing something to a Nullable it then does the reverse; creating a Nullable<T> struct based on whether it's null or not. (This is another thing that you couldn't do with a custom type.)

Because of these special compiler features it does a fairly good job of making it appear as if Nullable can actually be null, but the reality is that it is not actually null.

C# HasValue vs !=null

They both do the same thing, just pick one and stick with it so that you have consistency. There is nothing to gain by using either in the end.

HasValue() or ?? operand when dealing with nullable types in LINQ-to-Entity,

Nullable object must have a value is a runtime exception that occurs when you try to access .Value of a nullable with .HasValue false.

Your code:

ParentCommentId = lac.ParentCommentId ?? lac.ParentCommentId.Value

gets translated to:

if (lac.ParentCommentId.HasValue)
{
ParentCommentId = lac.ParentCommentId.Value;
}
else
{
ParentCommentId = lac.ParentCommentId.Value;
}

As you can see, both branches are doing the same and you would be accessing .Value even if HasValue is false (and will result in that exception).

The operator ?? is used to take the first not-null value. You can write

var value = value1 ?? value2 ?? value3;

The first of value1, value2, value3 that is not null will be assigned to value, if all are null then value will be set to null.

Explicit nullable types and where != null

The compiler is not "smart" enough to detect that the argument to Select() cannot be null in this case, as it only does static code analysis. For situations like these, the ! notation was introduced with nullable reference types. Applying an exclamation mark to an object tells the compiler to "shut up" about nullability warnings.

var result = aFunction()
.Where(data => data != null)
.Select(data => data!.Id).ToList();

This means that the compiler will not generate a warning that data could be null. This is helpful in all cases where you know that the value is not null, but the compiler (e.g. due to the complexity of the code) doesn't properly detect that.

Note that this really only removes the warning. If the value is, in fact, null, the code will behave as previously, so it would throw a NullReferenceException.



Related Topics



Leave a reply



Submit