{"id":4504,"date":"2014-03-30T12:29:30","date_gmt":"2014-03-30T12:29:30","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/c-casting-an-object-to-an-interface-collection-of-common-programming-errors\/"},"modified":"2014-03-30T12:29:30","modified_gmt":"2014-03-30T12:29:30","slug":"c-casting-an-object-to-an-interface-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/c-casting-an-object-to-an-interface-collection-of-common-programming-errors\/","title":{"rendered":"C# &#8211; Casting an object to an interface-Collection of common programming errors"},"content":{"rendered":"<p>Suppose we have an interface:<\/p>\n<pre><code>interface ICustomShape\n{\n}\n<\/code><\/pre>\n<p>and we have a class that inherits from the Shape class, and implements the interface:<\/p>\n<pre><code>public class CustomIsocelesTriangle : Shape, ICustomShape\n{\n\n}\n<\/code><\/pre>\n<p>How would I go about casting a CustomIsocelesTriangle to a ICustomShape object, for use on an &#8220;interface level?&#8221;<\/p>\n<pre><code>ICustomShape x = (ICustomShape)canvas.Children[0]; \/\/Gives runtime error: Unable to cast object of type 'program_4.CustomIsocelesTriangle' to type 'program_4.ICustomShape'.\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Suppose we have an interface: interface ICustomShape { } and we have a class that inherits from the Shape class, and implements the interface: public class CustomIsocelesTriangle : Shape, ICustomShape { } How would I go about casting a CustomIsocelesTriangle to a ICustomShape object, for use on an &#8220;interface level?&#8221; ICustomShape x = (ICustomShape)canvas.Children[0]; \/\/Gives [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-4504","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/4504","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=4504"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/4504\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=4504"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=4504"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=4504"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}