In C#, is there a difference between default(Nullable<long>)
(or default(long?)
) and default(long)
?
Long
is just an example, it can be any other struct
type.
The default value of a nullable value type represents null , that is, it's an instance whose Nullable<T>. HasValue property returns false .
The first NULL says that the column is nullable, i.e. accepts NULL . The second NULL (after DEFAULT ) is the default value. If you only have the default, but make the column reject nulls, then that default cannot be used.
There's no difference. The default value of any reference type is null . MSDN's C# reference page for default keyword: https://msdn.microsoft.com/en-us/library/25tdedf5.aspx.
Although using nullable reference types can introduce its own set of problems, I still think it's beneficial because it helps you find potential bugs and allows you to better express your intent in the code. For new projects, I would recommend you enable the feature and do your best to write code without warnings.
Well yes. The default value of a nullable or other reference type is null
while the default value for a long
or other value type is 0
(and any other members set to their defaults).
In this case:
default(Nullable<long>) == null default(long?) == null default(long) == 0L
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With