What is extension method in C#?

 Posted by Raja on 11/13/2008 | Category: C# Interview questions | Views: 9829

Extension method is a new feature in C# 3.0.

Extension methods allow existing classes to be extended without relying on inheritance or having to change the class's source code. This means that if you want to add some methods into the existing String class you can do it quite easily.

Here's a couple of rules to consider when deciding on whether or not to use extension methods:

* Extension methods cannot be used to override existing methods
* An extension method with the same name and signature as an instance method will not be called
* The concept of extension methods cannot be applied to fields, properties or events
* Use extension methods sparingly....overuse can be a bad thing!

For more details, see http://weblogs.asp.net/dwahlin/archive/2008/01/25/c-3-0-features-extension-methods.aspx

Asked In: Many Interviews | Alert Moderator 

Comments or Responses

Login to post response