{"id":4195,"date":"2014-03-30T09:11:16","date_gmt":"2014-03-30T09:11:16","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/elegant-solution-to-class-design-collection-of-common-programming-errors\/"},"modified":"2014-03-30T09:11:16","modified_gmt":"2014-03-30T09:11:16","slug":"elegant-solution-to-class-design-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/elegant-solution-to-class-design-collection-of-common-programming-errors\/","title":{"rendered":"Elegant solution to class design-Collection of common programming errors"},"content":{"rendered":"<p>It should be possible with extension methods by using generics. Multiple approaches are possible, but this one is simplest. Although you do get that <em>if<\/em><\/p>\n<pre><code>public static void Foo(this T objectC)\n     where T: C\n{\n     if(typeof(T)==typeof(B){ \/\/or for runtime check:     if(objectC is B)\n          \/\/specific\n     }\n}\n<\/code><\/pre>\n<p>you could then call Foo on any instance of A or B.<\/p>\n<p>You mention you don\u00b4t want if statements, but I&#8217;m not sure to which extend you&#8217;re trying to avoid that? The only way to completely avoid it, is to have 2 extension methods, one for A and one for B, (which in turn can call a common method for C), but I think you&#8217;re trying to avoid multiple extension methods?<\/p>\n<p><strong>edit<\/strong> If you absolutely want to prevent if&#8217;s, you&#8217;ll have to use multiple extension methods as shown in Frederik&#8217;s post. You could add an extension for the baseclass as well, that only gets called if the type is not known during compilation. But that would still need an if \ud83d\ude09<\/p>\n<pre><code>public static void Foo(this A a)\n{\n}\n\npublic static void Foo(this B b)\n{\n}\n\npublic static void Foo(this C c)\n{\n    if(c is A)\n       Foo((A)c);\n    else if(c is B)\n       Foo((B)c);\n    else\n       throw new NotSupportedException(c.GetType().FullName);\n}\n<\/code><\/pre>\n<p>If the type is always known at compile time, you can simply use the 2 extension methods for A en B.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>It should be possible with extension methods by using generics. Multiple approaches are possible, but this one is simplest. Although you do get that if public static void Foo(this T objectC) where T: C { if(typeof(T)==typeof(B){ \/\/or for runtime check: if(objectC is B) \/\/specific } } you could then call Foo on any instance of [&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-4195","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/4195","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=4195"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/4195\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=4195"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=4195"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=4195"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}