{"id":132,"date":"2014-08-08T10:46:53","date_gmt":"2014-08-08T10:46:53","guid":{"rendered":"http:\/\/mairwa.com\/wordpress\/?p=132"},"modified":"2014-08-08T10:46:53","modified_gmt":"2014-08-08T10:46:53","slug":"use-as-and-is-operators-for-casting","status":"publish","type":"post","link":"http:\/\/mairwa.com\/wordpress\/?p=132","title":{"rendered":"Use as and is Operators for casting"},"content":{"rendered":"<p>The\u00a0as operator is more efficient because it actually returns the cast value if the cast can be made successfully. The is operator returns only a Boolean value. It can therefore be used when we just want to determine an object&#8217;s type but do not have to actually cast it.<\/p>\n<p>Use the is operator to verify the type before performing a cast.<\/p>\n<p>Use the as operator and test for null\u00a0before referencing the variable.<\/p>\n<p>Use the as operator to test\u00a0an incompatible type.<\/p>\n<p>Use the as operator with a value type.\u00a0Note the implicit conversion to int? in\u00a0\u00a0the\u00a0\u00a0 method body.<\/p>\n<p>&nbsp;<\/p>\n<p>example&#8211;class SafeCasting<br \/>\n{<br \/>\nclass Animal<br \/>\n{<br \/>\npublic\u00a0void Eat() { Console.WriteLine(&#8220;Eating.&#8221;); }<br \/>\npublic\u00a0override\u00a0string ToString()<br \/>\n{<br \/>\nreturn\u00a0&#8220;I am an animal.&#8221;;<br \/>\n}<br \/>\n}<br \/>\nclass Mammal : Animal { }<br \/>\nclass Giraffe : Mammal { }<\/p>\n<p>class SuperNova { }<\/p>\n<p>static\u00a0void Main()<br \/>\n{<br \/>\nSafeCasting app = new SafeCasting();<\/p>\n<p>\/\/ Use the is operator to verify the type.\u00a0before performing a cast.<br \/>\nGiraffe g = new Giraffe();<br \/>\napp.UseIsOperator(g);<\/p>\n<p>\/\/ Use the as operator and test for null\u00a0before referencing the variable.<br \/>\napp.UseAsOperator(g);<\/p>\n<p>\/\/ Use the as operator to test\u00a0an incompatible type.<br \/>\nSuperNova sn = new SuperNova();<br \/>\napp.UseAsOperator(sn);<\/p>\n<p>\/\/ Use the as operator with a value type.<br \/>\n\/\/ Note the implicit conversion to int? in<br \/>\n\/\/ the method body.<br \/>\nint i = 5;<br \/>\napp.UseAsWithNullable(i);<\/p>\n<p>double d = 9.78654;<br \/>\napp.UseAsWithNullable(d);<\/p>\n<p>\/\/ Keep the console window open in debug mode.<br \/>\nSystem.Console.WriteLine(&#8220;Press any key to exit.&#8221;);<br \/>\nSystem.Console.ReadKey();<br \/>\n}<\/p>\n<p>void UseIsOperator(Animal a)<br \/>\n{<br \/>\nif (a is Mammal)<br \/>\n{<br \/>\nMammal m = (Mammal)a;<br \/>\nm.Eat();<br \/>\n}<br \/>\n}<\/p>\n<p>void UseAsOperator(object o)<br \/>\n{<br \/>\nMammal m = o as Mammal;<br \/>\nif (m != null)<br \/>\n{<br \/>\nConsole.WriteLine(m.ToString());<br \/>\n}<br \/>\nelse<br \/>\n{<br \/>\nConsole.WriteLine(&#8220;{0} is not a Mammal&#8221;, o.GetType().Name);<br \/>\n}<br \/>\n}<\/p>\n<p>void UseAsWithNullable(System.ValueType val)<br \/>\n{<br \/>\nint? j = val as\u00a0int?;<br \/>\nif (j != null)<br \/>\n{<br \/>\nConsole.WriteLine(j);<br \/>\n}<br \/>\nelse<br \/>\n{<br \/>\nConsole.WriteLine(&#8220;Could not convert &#8221; + val.ToString());<br \/>\n}<br \/>\n}<br \/>\n}<\/p>\n","protected":false},"excerpt":{"rendered":"<p class=\"excerpt\">The\u00a0as operator is more efficient because it actually returns the cast value if the cast can be made successfully. The is operator returns only a Boolean value. It can therefore be used when we just want to determine an object&#8217;s type but do not have to actually cast it. Use the is operator to verify the type before performing a&hellip; <a href=\"http:\/\/mairwa.com\/wordpress\/?p=132\">Read more &rarr;<\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-132","post","type-post","status-publish","format-standard","hentry","category-c-vb","xfolkentry"],"_links":{"self":[{"href":"http:\/\/mairwa.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/132","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/mairwa.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/mairwa.com\/wordpress\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/mairwa.com\/wordpress\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/mairwa.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=132"}],"version-history":[{"count":0,"href":"http:\/\/mairwa.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/132\/revisions"}],"wp:attachment":[{"href":"http:\/\/mairwa.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=132"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/mairwa.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=132"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/mairwa.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=132"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}