{"id":221,"date":"2013-08-20T11:27:52","date_gmt":"2013-08-20T14:27:52","guid":{"rendered":"http:\/\/gianfratti.com\/?p=221"},"modified":"2013-08-20T11:28:33","modified_gmt":"2013-08-20T14:28:33","slug":"o-operador","status":"publish","type":"post","link":"https:\/\/gianfratti.com\/index.php\/o-operador\/","title":{"rendered":"O operador ??"},"content":{"rendered":"<p><span style=\"font-family: Trebuchet MS; font-size: large;\"><strong>Sintaxe: vari\u00e1vel ?? valor padr\u00e3o<\/strong><\/span><\/p>\n<p><span style=\"font-family: Trebuchet MS; font-size: medium;\">O operador<strong> ?? <\/strong>retorna o operando \u00e0 esquerda da express\u00e3o se o valor n\u00e3o for nulo; se o valor for nulo ele retorna o operando \u00e0 direita da express\u00e3o.<!--more--><\/span><\/p>\n<table border=\"0\">\n<tbody>\n<tr>\n<td bgcolor=\"#FFFFC4\"><span style=\"font-family: Trebuchet MS;\"><span style=\"font-family: Trebuchet MS;\">object a = null;<br \/>\nobject d = new object();<br \/>\nobject e;<\/span><\/span>if (a != null)<br \/>\ne = a;<br \/>\nelse<br \/>\ne = d;<\/td>\n<td bgcolor=\"#E2E2E2\"><span style=\"font-family: Trebuchet MS;\"><span style=\"font-family: Trebuchet MS;\">object a = null;<br \/>\nobject d = new object();<br \/>\nobject e;<\/span><\/span><strong>object e = c ?? d;<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><span style=\"font-family: Trebuchet MS; font-size: medium;\">Abaixo temos um exemplo mais pr\u00e1tico:<\/span><\/p>\n<table border=\"0\">\n<tbody>\n<tr>\n<td bgcolor=\"#FFFFBB\"><span style=\"font-family: Trebuchet MS;\">string mensagem = null;<br \/>\nConsole.WriteLine(mensagem ?? &#8220;Seu Texto&#8221;);<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><span style=\"font-family: Trebuchet MS; font-size: medium;\">Como mensagem possui um valor null ser\u00e1 exibida a mensagem <strong>&#8220;Seu Texto&#8221;<\/strong> definida ap\u00f3s o operador <strong>??<\/strong>.<\/span><\/p>\n<p><span style=\"font-family: Trebuchet MS; font-size: medium;\">Vamos agora combinar a utiliza\u00e7\u00e3o dos dois operadores:<\/span><\/p>\n<table border=\"0\">\n<tbody>\n<tr>\n<td bgcolor=\"#FFFFBB\"><span style=\"font-family: Trebuchet MS;\"><span style=\"font-family: Trebuchet MS;\">string conceito = null ;<br \/>\nint nota = 5;<br \/>\nstring resultado = &#8220;&#8221;;<\/span><\/span>resultado = nota &gt;= 5 ? conceito ?? &#8221; Aprovado &#8221; : &#8221; Reprovado &#8220;;<br \/>\nConsole.WriteLine(resultado);<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><span style=\"font-family: Trebuchet MS; font-size: medium;\">O que ser\u00e1 exibido ???<\/span><\/p>\n<p><span style=\"font-family: Trebuchet MS; font-size: medium;\">O operador ? ir\u00e1 verificar se a express\u00e3o <\/span><span style=\"font-family: Trebuchet MS; font-size: large;\">nota &gt;= 5 <\/span><span style=\"font-family: Trebuchet MS; font-size: medium;\">\u00e9 verdadeira, neste caso, a primeira express\u00e3o ser\u00e1 avaliada:<\/span><\/p>\n<p><span style=\"font-family: Trebuchet MS; font-size: large;\">conceito ?? &#8221; Reprovado &#8220;<\/span><\/p>\n<p><span style=\"font-family: Trebuchet MS; font-size: medium;\">Nesta express\u00e3o temos o operador <b>??<\/b> e como o valor da vari\u00e1vel conceito \u00e9 <b>null<\/b> ent\u00e3o ser\u00e1 exibida a string <\/span><span style=\"font-family: Trebuchet MS; font-size: large;\"><strong>&#8221; Aprovado &#8220;<\/strong><\/span><span style=\"font-family: Trebuchet MS; font-size: medium;\">;<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Sintaxe: vari\u00e1vel ?? valor padr\u00e3o O operador ?? retorna o operando \u00e0 esquerda da express\u00e3o se o valor n\u00e3o for nulo; se o valor for nulo ele retorna o operando \u00e0 direita da express\u00e3o.<\/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-221","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\/221","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=221"}],"version-history":[{"count":2,"href":"https:\/\/gianfratti.com\/index.php\/wp-json\/wp\/v2\/posts\/221\/revisions"}],"predecessor-version":[{"id":223,"href":"https:\/\/gianfratti.com\/index.php\/wp-json\/wp\/v2\/posts\/221\/revisions\/223"}],"wp:attachment":[{"href":"https:\/\/gianfratti.com\/index.php\/wp-json\/wp\/v2\/media?parent=221"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gianfratti.com\/index.php\/wp-json\/wp\/v2\/categories?post=221"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gianfratti.com\/index.php\/wp-json\/wp\/v2\/tags?post=221"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}