{"id":226,"date":"2013-08-20T11:34:33","date_gmt":"2013-08-20T14:34:33","guid":{"rendered":"http:\/\/gianfratti.com\/?p=226"},"modified":"2013-08-20T11:36:02","modified_gmt":"2013-08-20T14:36:02","slug":"nullabe-types-ou-tipos-anulaveis","status":"publish","type":"post","link":"https:\/\/gianfratti.com\/index.php\/nullabe-types-ou-tipos-anulaveis\/","title":{"rendered":"Nullabe Types ou Tipos Anul\u00e1veis"},"content":{"rendered":"<p><span style=\"font-family: Trebuchet MS;\">Existem situa\u00e7\u00f5es, especialmente quando se trabalha com bases de dados, onde voc\u00ea quer indicar que uma vari\u00e1vel n\u00e3o det\u00e9m atualmente um valor v\u00e1lido. Para tipos de refer\u00eancia, voc\u00ea pode fazer isso com facilidade definindo a vari\u00e1vel para <b>null<\/b>.<!--more--><\/span><\/p>\n<p><span style=\"color: #000000; font-family: Trebuchet MS; font-size: medium;\">Os <strong>nullabe Types ou Tipos Anul\u00e1veis<\/strong> permitem que voc\u00ea crie um tipo por valor que pode ser marcado como v\u00e1lido ou inv\u00e1lido de forma que voc\u00ea tenha certeza que a vari\u00e1vel seja v\u00e1lida antes de ser usada.Tipos de valores regulares s\u00e3o chamados de tipos n\u00e3o anul\u00e1veis.<\/span><\/p>\n<p><span style=\"color: #ff0000; font-family: Trebuchet MS; font-size: medium;\"><em><strong>Obs: Tipos anul\u00e1vel s\u00e3o inst\u00e2ncias da struct System.Nullable <\/strong><\/em><\/span><span style=\"color: #000000; font-family: Trebuchet MS; font-size: medium;\">.<\/span><\/p>\n<p>Um tipo anul\u00e1vel \u00e9 sempre baseado em outro tipo, chamado tipo subjacente, que j\u00e1 foi declarado e sobre os quais temos que:<\/p>\n<ul>\n<li><span style=\"color: #000000; font-family: Trebuchet MS; font-size: medium;\">Voc\u00ea pode criar um tipo anul\u00e1vel a partir de qualquer tipo de valor, incluindo o pr\u00e9-definido, tipos simples;<\/span><\/li>\n<li><span style=\"color: #000000; font-family: Trebuchet MS; font-size: medium;\">Voc\u00ea n\u00e3o pode criar um tipo anul\u00e1vel a partir de um tipo de refer\u00eancia ou de outro tipo anul\u00e1vel;<\/span><\/li>\n<li><span style=\"color: #000000; font-family: Trebuchet MS; font-size: medium;\">Voc\u00ea n\u00e3o declara explicitamente um tipo anul\u00e1vel em seu c\u00f3digo. Em vez disso, voc\u00ea declara uma vari\u00e1vel de um tipo anul\u00e1vel. O compilador cria implicitamente o tipo anul\u00e1vel para voc\u00ea;<\/span><\/li>\n<\/ul>\n<p>P<span style=\"font-family: Trebuchet MS; font-size: medium;\">or exemplo, o c\u00f3digo a seguir declara uma vari\u00e1vel do tipo <b>int<\/b> anul\u00e1vel. Observe que o sufixo \u00e9 anexado ao nome do tipo, n\u00e3o ao nome da vari\u00e1vel.<\/span><\/p>\n<p><span style=\"color: #0000ff; font-family: Trebuchet MS; font-size: large;\"><strong>int? meuInteiro = 28;<\/strong><\/span><\/p>\n<p><span style=\"font-family: Trebuchet MS; font-size: medium;\">Com essa instru\u00e7\u00e3o de declara\u00e7\u00e3o, o compilador cuida de ambas produzindo o tipo anul\u00e1vel e criando uma vari\u00e1vel desse tipo.<\/span><\/p>\n<p><span style=\"font-family: Trebuchet MS; font-size: medium;\">Veja a seguir mais exemplos de tipos anul\u00e1veis:<\/span><\/p>\n<p><span style=\"color: #0000ff; font-family: Trebuchet MS; font-size: large;\"><b>int? i = 10;<br \/>\ndouble? d1 = 3.14;<br \/>\nbool? flag = null;<br \/>\nchar? letter = &#8216;a&#8217;;<br \/>\nint?[] arr = new int?[10];<\/b><\/span><\/p>\n<p><span style=\"color: #ff0000; font-family: Trebuchet MS; font-size: large;\"><b>Obs :Os tipos Anul\u00e1veis tamb\u00e9m podem ser declarados da seguinte forma:\u00a0 <\/b><\/span><span style=\"color: #0000ff; font-family: Trebuchet MS; font-size: large;\"><b>System.Nullable&lt;T&gt; vari\u00e1vel\u00a0<\/b><\/span><span style=\"color: #800080; font-family: Trebuchet MS; font-size: large;\"><b>Exemplo:<\/b><\/span><span style=\"font-family: Trebuchet MS; font-size: large;\">System.<\/span><span style=\"color: #2b91af; font-family: Trebuchet MS; font-size: large;\">Nullable<\/span><span style=\"font-family: Trebuchet MS; font-size: large;\">&lt;<\/span><span style=\"color: #0000ff; font-family: Trebuchet MS; font-size: large;\">int<\/span><span style=\"font-family: Trebuchet MS; font-size: large;\">&gt; x = 9;<\/span><\/p>\n<p><b><span style=\"color: #ff0000; font-family: Trebuchet MS; font-size: medium;\"><i>Aten\u00e7\u00e3o !!!\u00a0 Usar um tipo anul\u00e1vel \u00e9 como usar uma vari\u00e1vel de qualquer outro tipo.<\/i><\/span><\/b><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Existem situa\u00e7\u00f5es, especialmente quando se trabalha com bases de dados, onde voc\u00ea quer indicar que uma vari\u00e1vel n\u00e3o det\u00e9m atualmente um valor v\u00e1lido. Para tipos de refer\u00eancia, voc\u00ea pode fazer isso com facilidade definindo a vari\u00e1vel para null. <span class=\"ellipsis\">&hellip;<\/span> <span class=\"more-link-wrap\"><a href=\"https:\/\/gianfratti.com\/index.php\/nullabe-types-ou-tipos-anulaveis\/\" class=\"more-link\"><span>Read More &rarr;<\/span><\/a><\/span><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_exactmetrics_skip_tracking":false,"_exactmetrics_sitenote_active":false,"_exactmetrics_sitenote_note":"","_exactmetrics_sitenote_category":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[2],"tags":[],"class_list":["post-226","post","type-post","status-publish","format-standard","hentry","category-net-framework"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/gianfratti.com\/index.php\/wp-json\/wp\/v2\/posts\/226","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gianfratti.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/gianfratti.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/gianfratti.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/gianfratti.com\/index.php\/wp-json\/wp\/v2\/comments?post=226"}],"version-history":[{"count":2,"href":"https:\/\/gianfratti.com\/index.php\/wp-json\/wp\/v2\/posts\/226\/revisions"}],"predecessor-version":[{"id":228,"href":"https:\/\/gianfratti.com\/index.php\/wp-json\/wp\/v2\/posts\/226\/revisions\/228"}],"wp:attachment":[{"href":"https:\/\/gianfratti.com\/index.php\/wp-json\/wp\/v2\/media?parent=226"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gianfratti.com\/index.php\/wp-json\/wp\/v2\/categories?post=226"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gianfratti.com\/index.php\/wp-json\/wp\/v2\/tags?post=226"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}