{"id":450,"date":"2023-08-19T10:58:28","date_gmt":"2023-08-19T16:58:28","guid":{"rendered":"https:\/\/kop.lat\/blog\/?p=450"},"modified":"2023-08-21T09:55:19","modified_gmt":"2023-08-21T15:55:19","slug":"upcasting-and-downcasting-in-c-explained-with-animals","status":"publish","type":"post","link":"https:\/\/kop.lat\/blog\/upcasting-and-downcasting-in-c-explained-with-animals\/","title":{"rendered":"Upcasting and Downcasting in C# explained with Animals"},"content":{"rendered":"\n<p><\/p>\n\n\n\n<p><strong>Upcasting:<\/strong><br>Imagine you have a group of animals: dogs, cats, and rabbits. <\/p>\n\n\n\n<p>Now, think about how these animals are related. <\/p>\n\n\n\n<p>Dogs and cats are animals, and rabbits are also animals. <\/p>\n\n\n\n<p><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-red-color\">In programming, upcasting is when you treat a more specialized object as if it were a more general object. <\/mark><\/p>\n\n\n\n<p>You&#8217;re moving up the hierarchy. Just like how you can say &#8220;all dogs are animals,&#8221; in programming, you can say &#8220;all instances of a derived class are instances of the base class.&#8221;<\/p>\n\n\n\n<p>For example, if you have classes <code>Animal<\/code>, <code>Dog<\/code>, and <code>Cat<\/code>, where <code>Dog<\/code> and <code>Cat<\/code> are derived from <code>Animal<\/code>, upcasting would look like this:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Animal dog = new Dog(); \/\/ Treating a Dog as an Animal<\/code><\/pre>\n\n\n\n<p>You&#8217;re &#8220;upcasting&#8221; a <code>Dog<\/code> object to an <code>Animal<\/code> reference, treating it as the more general base class.<\/p>\n\n\n\n<p><strong>Downcasting:<\/strong><br>Now let&#8217;s say you have an <code>Animal<\/code> object, but you know that it&#8217;s actually a <code>Dog<\/code>. <\/p>\n\n\n\n<p>You can&#8217;t directly call <code>bark()<\/code> on an <code>Animal<\/code>, but you can call it on a <code>Dog<\/code>. <\/p>\n\n\n\n<p><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-red-color\">So, downcasting is the opposite of upcasting. It&#8217;s when you treat a more general object as if it were a more specialized object. You&#8217;re moving down the hierarchy.<\/mark><\/p>\n\n\n\n<p>Continuing with our animals, if you have an <code>Animal<\/code> object but you know it&#8217;s a <code>Dog<\/code>, you can do this:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Animal animal = new Dog(); \/\/ Animal reference, but it's actually a Dog\nDog myDog = (Dog)animal; \/\/ Downcasting to treat the Animal as a Dog<\/code><\/pre>\n\n\n\n<p>Here, you&#8217;re &#8220;downcasting&#8221; the <code>Animal<\/code> reference to a <code>Dog<\/code> reference so that you can access <code>Dog<\/code>-specific behavior.<\/p>\n\n\n\n<p>While upcasting and downcasting can be useful, it&#8217;s important to be careful. <\/p>\n\n\n\n<p>If you try to downcast to a type that&#8217;s not actually what the object is, you might encounter runtime errors. <\/p>\n\n\n\n<p>In the animal analogy, trying to treat a <code>Cat<\/code> as a <code>Dog<\/code> could lead to some unexpected and confusing behavior.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Upcasting:Imagine you have a group of animals: dogs, cats, and rabbits. Now, think about how these animals are related. Dogs and cats are animals, and rabbits are also animals. In programming, upcasting is when you treat a more specialized object as if it were a more general object. You&#8217;re moving up the hierarchy. Just like [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":459,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[36,41,1,51,12],"tags":[],"_links":{"self":[{"href":"https:\/\/kop.lat\/blog\/wp-json\/wp\/v2\/posts\/450"}],"collection":[{"href":"https:\/\/kop.lat\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kop.lat\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kop.lat\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kop.lat\/blog\/wp-json\/wp\/v2\/comments?post=450"}],"version-history":[{"count":1,"href":"https:\/\/kop.lat\/blog\/wp-json\/wp\/v2\/posts\/450\/revisions"}],"predecessor-version":[{"id":451,"href":"https:\/\/kop.lat\/blog\/wp-json\/wp\/v2\/posts\/450\/revisions\/451"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/kop.lat\/blog\/wp-json\/wp\/v2\/media\/459"}],"wp:attachment":[{"href":"https:\/\/kop.lat\/blog\/wp-json\/wp\/v2\/media?parent=450"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kop.lat\/blog\/wp-json\/wp\/v2\/categories?post=450"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kop.lat\/blog\/wp-json\/wp\/v2\/tags?post=450"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}