{"id":4477,"date":"2014-03-30T11:25:37","date_gmt":"2014-03-30T11:25:37","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/concept-of-c-polymorphism-collection-of-common-programming-errors\/"},"modified":"2014-03-30T11:25:37","modified_gmt":"2014-03-30T11:25:37","slug":"concept-of-c-polymorphism-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/concept-of-c-polymorphism-collection-of-common-programming-errors\/","title":{"rendered":"Concept of C# polymorphism-Collection of common programming errors"},"content":{"rendered":"<blockquote>\n<p>Use &#8216;new&#8217; modifier, which function will be called is determined when it is compiled.The program will choose the object&#8217;s declared type to call its function.(As above, the f&#8217;s declared type is father ,so using &#8216;new&#8217; modifier make the output to show &#8216;father&#8217;.<\/p>\n<\/blockquote>\n<p>Not really. The decision is still made at execution time, but the <code>new<\/code> method does <em>not<\/em> override the virtual method in the base class. This is most easily shown by extending your example somewhat:<\/p>\n<pre><code>using System;\n\nclass Base\n{\n    public virtual void Foo()\n    {\n        Console.WriteLine(\"Base.Foo\");\n    }\n}\n\nclass Derived : Base\n{\n    public override void Foo()\n    {\n        Console.WriteLine(\"Derived.Foo\");\n    }\n}\n\nclass MoreDerived : Derived\n{\n    public new void Foo()\n    {\n        Console.WriteLine(\"MoreDerived.Foo\");\n    }\n}\n\nclass Test\n{\n    static void Main()\n    {\n        Base x = new MoreDerived();\n        x.Foo(); \/\/ Prints Derived.Foo\n    }\n}\n<\/code><\/pre>\n<p>Here, at <em>compile time<\/em> the decision is made to call the most overridden implementation of <code>Base.Foo<\/code> &#8211; if there were multiple <code>Foo<\/code> signatures, the decision about which signature to use would be taken, for example. Which implementation will be &#8220;the most overridden&#8221; is unknown at this point, of course.<\/p>\n<p>At <em>execution time<\/em>, the CLR will find that most overridden implementation based on the actual type of the target object &#8211; which is <code>MoreDerived<\/code>. But <code>MoreDerived.Foo<\/code> doesn&#8217;t override <code>Base.Foo<\/code>&#8230; whereas <code>Derived.Foo<\/code> does, so the implementation in <code>Derived<\/code> is the one which is actually executed.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Use &#8216;new&#8217; modifier, which function will be called is determined when it is compiled.The program will choose the object&#8217;s declared type to call its function.(As above, the f&#8217;s declared type is father ,so using &#8216;new&#8217; modifier make the output to show &#8216;father&#8217;. Not really. The decision is still made at execution time, but the new [&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-4477","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/4477","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=4477"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/4477\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=4477"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=4477"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=4477"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}