¿Cuál es el valor predeterminado del tipo nullable "int?"(incluyendo signo de interrogación)?
En C#, ¿cuál es el valor predeterminado de una variable de instancia de clase de tipo int?
?
Por ejemplo, en el siguiente código, ¿qué valor tendrá MyNullableInt
si nunca se asigna explícitamente?
class MyClass
{
public int? MyNullableInt;
}
(Parece probable que la respuesta sea casi con certeza null
o 0
, pero ¿cuál de esos es?)
68
3 answers
El valor predeterminado para int?
} y para cualquier tipo nullable que use el "type?"declaración is es null
.
Por qué este es el caso:
-
int?
es azúcar sintáctica para el tipo Nullable(donde T es int
), una estructura. (referencia ) - El tipo
Nullable<T>
tiene un miembro bool HasValue, que cuandofalse
, hace que la instanciaNullable<T>
"actúe como" un valornull
. En particular, el Nullable.El método Igual a se anula a devuelvetrue
cuando se compara unNullable<T>
conHasValue == false
con un valor realnull
. - De la Especificación del lenguaje C# 11.3.4, el valor predeterminado inicial de una instancia de estructura es todos los campos de tipo de valor de esa estructura establecidos en su valor predeterminado, y todos los campos de tipo de referencia de esa estructura establecidos en
null
. - El valor predeterminado de una variable
bool
en C # esfalse
(referencia ). Por lo tanto, la propiedadHasValue
de una instancia predeterminadaNullable<T>
esfalse
; que a su vez hace que la propia instanciaNullable<T>
actúe comonull
.
96
Author: Jon Schneider,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-03-19 19:59:00
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-03-19 19:59:00
Me pareció importante compartir el método Nullable<T>.GetValueOrDefault()
que es particularmente útil cuando se trabaja con cálculos matemáticos que utilizan Nullable<int>
aka int?
valores. Hay muchas veces en las que no tiene que verificar la propiedad HasValue
y puede usar GetValueOrDefault()
en su lugar.
var defaultValueOfNullableInt = default(int?);
Console.WriteLine("defaultValueOfNullableInt == {0}", (defaultValueOfNullableInt == null) ? "null" : defaultValueOfNullableInt.ToString());
var defaultValueOfInt = default(int);
Console.WriteLine("defaultValueOfInt == {0}", defaultValueOfInt);
Console.WriteLine("defaultValueOfNullableInt.GetValueOrDefault == {0}", defaultValueOfNullableInt.GetValueOrDefault());
14
Author: Brian Ogden,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-11-13 22:38:15
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-11-13 22:38:15
var x = default (int?);
Console.WriteLine("x == {0}", (x == null) ? "null" : x.ToString());
0
Author: Rufus L,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-03-19 20:02:02
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-03-19 20:02:02