C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Communities   Interview   Jobs   Projects   Offshore Development    
Silverlight Tutorials | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Revenue Sharing |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...

New Feature: Community Sites: Create your own .NET community website and start earning from Google AdSense ! It's Free !




The new modifier in C#


Posted Date: 31 Jan 2008    Resource Type: Code Snippets    Category: Console Applications

Posted By: VasanthRaj       Member Level: Gold
Rating:     Points: 10





Similarly to C++ , C# allow redefinition of inherited methods in the derived classes at the cost of hiding the inherited ones.

Although hiding is not an error, but it does cause the compiler to issue a warning. To suppress this warning you have to include the new modifier in the derived method.

The real benefit of this feature is to be sure that you intentionally want to hide the base method and this did not happen by accident or misspelling.

Conversely, if you declare a method as new and it does not really hide a base method, the compiler will also issue a warning, that can be suppressed by removing the new modifier.




Consider the following example :



public class baseHello{

public void sayHello(){

System.Console.WriteLine("base says hello");

}

}


class derivedHello:BaseHello{

public void sayHello(){

System.Console.WriteLine("derived says hello");

}

}




The preceding code will compile fine but the compiler warns you that method derivedHello.sayHello() hides the method baseHello.sayHello() :

warning CS0114: 'derivedHello.sayHello()' hides inherited member 'baseHello.sayHello()'. To make the current method override that implementation, add the override keyword. Otherwise add the new keyword.

As the warning suggest it is preferable to use new like the following.



class derivedHello:BaseHello{

new public void sayHello(){

System.Console.WriteLine("derived says hello");

}

}






Responses


No responses found. Be the first to respond and make money from revenue sharing program.

Feedbacks      
Popular Tags   What are tags ?   Search Tags  
(No tags found.)

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Generation of Prime Number
Previous Resource: LinkList using C#
Return to Discussion Resource Index
Post New Resource
Category: Console Applications


Post resources and earn money!
 
Related Resources



dotNet Slackers   BizTalk Adaptors    Web Design

masks masks masks

Contact Us    Privacy Policy    Terms Of Use