{"id":29,"date":"2016-02-10T20:59:03","date_gmt":"2016-02-11T01:59:03","guid":{"rendered":"http:\/\/langstonsoftware.com\/?p=29"},"modified":"2016-02-13T13:08:25","modified_gmt":"2016-02-13T18:08:25","slug":"overloaded-inheritance-in-c","status":"publish","type":"post","link":"https:\/\/langstonsoftware.com\/2016\/02\/10\/overloaded-inheritance-in-c\/","title":{"rendered":"Overloaded Inheritance in C#"},"content":{"rendered":"
\r\nclass Foo {\r\n public void Who( int i ) {\r\n Console.Write("Foo");\r\n }\r\n}\r\n\r\nclass Bar : Foo {\r\n public void Who( double d ) {\r\n Console.Write("Bar");\r\n }\r\n}\r\n\r\nvar bar = new Bar();\r\nbar.Who( (int) 1 );\r\n\/\/Outputs "Bar" to the Console\r\n<\/pre>\nWhy does the compiler use the double<\/em> version of the method when we passed an integer<\/em>?<\/p>\n
The answer is like a super bowl, it comes in two halves.<\/p>\n
\n
- There is an implicit conversion from double<\/em> to integer<\/em>. This makes Who()<\/em> defined in Bar<\/em> good enough for the compiler, or as the reference docs below call an applicable method<\/strong>.<\/li>\n
- The compiler won’t normally look deeper into the inheritance chain if an applicable method can be found. This was done to combat the brittle base class problem<\/strong> found in many programming languages. <\/li>\n<\/ol>\n
Further reading can be found at these MSDN references:
\nhttps:\/\/blogs.msdn.microsoft.com\/ericlippert\/2007\/09\/04\/future-breaking-changes-part-three\/<\/a>
\nhttps:\/\/msdn.microsoft.com\/en-us\/library\/aa691356(v=vs.71).aspx<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"class Foo { public void Who( int i ) { Console.Write("Foo"); } } class Bar : Foo { public void Who( double d ) { Console.Write("Bar"); } } var bar = new Bar(); bar.Who( (int) 1 ); \/\/Outputs "Bar" to the Console Why does the compiler use the double version of the method when we …<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"nf_dc_page":"","jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","enabled":false},"version":2}},"categories":[2],"tags":[],"class_list":["post-29","post","type-post","status-publish","format-standard","hentry","category-c"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p6Os38-t","_links":{"self":[{"href":"https:\/\/langstonsoftware.com\/wp-json\/wp\/v2\/posts\/29"}],"collection":[{"href":"https:\/\/langstonsoftware.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/langstonsoftware.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/langstonsoftware.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/langstonsoftware.com\/wp-json\/wp\/v2\/comments?post=29"}],"version-history":[{"count":4,"href":"https:\/\/langstonsoftware.com\/wp-json\/wp\/v2\/posts\/29\/revisions"}],"predecessor-version":[{"id":34,"href":"https:\/\/langstonsoftware.com\/wp-json\/wp\/v2\/posts\/29\/revisions\/34"}],"wp:attachment":[{"href":"https:\/\/langstonsoftware.com\/wp-json\/wp\/v2\/media?parent=29"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/langstonsoftware.com\/wp-json\/wp\/v2\/categories?post=29"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/langstonsoftware.com\/wp-json\/wp\/v2\/tags?post=29"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}