{"id":501,"date":"2010-01-14T10:33:58","date_gmt":"2010-01-14T10:33:58","guid":{"rendered":"http:\/\/www.codingfriends.com\/?p=501"},"modified":"2010-01-14T10:33:58","modified_gmt":"2010-01-14T10:33:58","slug":"use-the-new-keyword-if-hiding-was-intendedcs0108","status":"publish","type":"post","link":"https:\/\/www.codingfriends.com\/index.php\/2010\/01\/14\/use-the-new-keyword-if-hiding-was-intendedcs0108\/","title":{"rendered":"Use the new keyword if hiding was intended(CS0108)"},"content":{"rendered":"<p>If you are using the inheritance in c# and are wanting to override (hide) the base class method but forget to use the &#8216;new&#8217; keyword then you will get the warning.<\/p>\n<pre lang=\"csharp\">\r\nUse the new keyword if hiding was intended(CS0108)\r\n<\/pre>\n<p>An example of this in coding syntax would be something like <\/p>\n<pre lang=\"csharp\">\r\nclass BC \r\n{\r\n  public void DisplayMe()\r\n  {\r\n    System.Console.WriteLine(\"Display me from BC\");\r\n  }\r\n}\r\n\/\/ inherited class from BC\r\nclass NewBC : BC \r\n{\r\n  public void DisplayMe()\r\n  {\r\n    System.Console.WriteLine(\"Display me from the NewBC :) yeppy\");\r\n  }\r\n}\r\n\r\nNewBC NBC = new NewBC();\r\nNBC.DisplayMe();\r\n<\/pre>\n<p>and the output would be <\/p>\n<pre lang=\"bash\">\r\nDisplay me from the NewBC :) yeppy\r\n<\/pre>\n<p>If you note that the base class in the NBC has not been called from the NBC.DisplayMe() method e.g. &#8220;Display Me from BC&#8221;<\/p>\n<p>But you will get the warning message from the compiler<\/p>\n<pre lang=\"csharp\">\r\nUse the new keyword if hiding was intended(CS0108)\r\n<\/pre>\n<p>So all you do is to put the keyword &#8216;new&#8217; before the overriding method e.g.<\/p>\n<pre lang=\"csharp\">\r\n\/\/ inherited class from BC\r\nclass NewBC : BC \r\n{\r\n  new public void DisplayMe()\r\n  {\r\n    System.Console.WriteLine(\"Display me from the NewBC :) yeppy\");\r\n  }\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>If you are using the inheritance in c# and are wanting to override (hide) the base class method but forget to use the &#8216;new&#8217; keyword then you will get the warning. Use the new keyword if hiding was intended(CS0108) An example of this in coding syntax would be something like class BC { public void &hellip; <a href=\"https:\/\/www.codingfriends.com\/index.php\/2010\/01\/14\/use-the-new-keyword-if-hiding-was-intendedcs0108\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Use the new keyword if hiding was intended(CS0108)<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12],"tags":[56],"class_list":["post-501","post","type-post","status-publish","format-standard","hentry","category-c_sharp","tag-inheritance"],"_links":{"self":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts\/501","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/comments?post=501"}],"version-history":[{"count":1,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts\/501\/revisions"}],"predecessor-version":[{"id":502,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts\/501\/revisions\/502"}],"wp:attachment":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/media?parent=501"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/categories?post=501"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/tags?post=501"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}