Difference between default value (int?) Vs (int?) Null

Is there any difference using default(int?)or (int?)nullto assign a variable?

Same?

Or are there some pros and cons to using each method?

+4
source share
2 answers

They are exactly the same as new int?().

If you just assign a variable, you usually will not need it. I would just use:

int? x = null;

eg.

The time that I most often need one of these expressions is a conditional statement, for example

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

You cannot use nullin this scenario, because the compiler cannot infer the type of expression. (Perhaps this would be a useful addition to the language, mind you ...)

+9

. . null , 0. nullable, (int?)null. IL .

0

Source: https://habr.com/ru/post/1659850/


All Articles