int n == 0; if (n == null) { Console.WriteLine("......"); }
Is it true that the result of expression (n == null
) is always false
since
a value of type int
is never equal to null
of type int?
(see warning below)
Warning CS0472 The result of the expression is always 'false' since a value of type 'int' is never equal to 'null' of type 'int?'
Java primitive types (such as int , double , or float ) cannot have null values, which you must consider in choosing your result expression and host expression types.
Value types like int contain direct values rather than references like ReferenceType . A value cannot be null but a reference can be null which means it is pointing to nothing. A value cannot have nothing in it.
Int is a value type so it cannot be null. Empty value of int depends on the logic of your application - it can be 0 or -1 or int. MinValue (well, technically, any number).
No difference. int? is just shorthand for Nullable<int> , which itself is shorthand for Nullable<Int32> . Compiled code will be exactly the same whichever one you choose to use.
If you want your integer variable to allow null values, declare it to be a nullable type:
int? n = 0;
Note the ?
after int, which means that type can have the value null
. Nullable types were introduced with v2.0 of the .NET Framework.
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