Nullable<Int> VS. Int? - Is There Any Difference

Nullableint vs. int? - Is there any difference?

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.

Difference between default(int?) vs (int?)null

They're exactly the same thing, as is new int?().

If you're just assigning a variable, you normally wouldn't need it at all though. I'd just use:

int? x = null;

for example.

The time I most often need one of these expressions is the conditional operator, e.g.

int y = ...;
int? z = condition ? default(int?) : y;

You can't use null in that scenario as the compiler can't infer the type of the expression. (Arguably that would be a useful addition to the language, mind you...)

What is the difference between ?int $number and int $number = null?

It is important to distinguish between the two language features being discussed here, that is, type declarations and default argument values.

The first function is only using type declarations, this means that the input argument has to be of the type int or NULL.

The second function is using both type declarations and default argument values, this means that the argument has to be of the type int or NULL but if omitted it will default to NULL.

Take your first function, if you simply called test() without passing anything to it, you'd get:

PHP Fatal error: Uncaught ArgumentCountError: Too few arguments to function test() [...]

which is correct as the function expects either int or NULL but got neither whereas for the second, as you have defined the argument with a default value, it would run without errors.

Code

function test(?int $var) {
var_dump($var);
}

function test2(int $var = null) {
var_dump($var);
}

test(1); // fine
test(); // error
test2(1); // fine
test2(); // fine

As far as performance goes, the difference is probably negligible, nothing significant enough that would be a cause for concern.

Live Example

Repl

When int? and Nullableint are the same, why do they behave in a different way?

This is simply because ? and & are overloaded and can also indicate a conditional operator and "address of" respectively. The compiler needs to know what you mean. This fixes it:

var getaccess = (val is int?) & result;

The compiler message isn't entirely clear, but gives us the clues:

CS0214  Pointers and fixed size buffers may only be used in an unsafe 

(which is from the & result)

CS1003  Syntax error, ':' expected

(which is from the ?)

and:

CS1525  Invalid expression term ';'

(which is also from the ? because it expects a : {value if false} expression before the next semicolon)

Basically, without the parentheses it thinks you mean:

var getaccess = (val is int) ? (&result)

(with a missing expression for what to do if val is not an int)

Is there any difference between type? and Nullabletype?

If you look at the IL using Ildasm, you'll find that they both compile down to Nullable<bool>.

Explanation of int? vs int

int cannot be null.

int? is an alias for the Nullable<int> struct, which you can set to null.

How does comparison operator works with null int?

According to MSDN - it's down the page in the "Operators" section:

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 !=

So both a > b and a < b evaluate to false since a is null...

Nullableint vs. int? - Is there any difference?

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.

What's the difference between 'int?' and 'int' in C#?

int? is shorthand for Nullable<int>.

This may be the post you were looking for.



Related Topics



Leave a reply



Submit