What is Extension methods and how it works ?

Posted by Sksingh under C# category on | Points: 40 | Views : 1543
Extension method is new in c# 3.0 which allows creating new methods for the existing classes without breaking the current class.

Below method will be listing in String class , to get count of words in that particular string.

public static int NoOfWrods(this string instance)
return instance.Split(' ',',','.',';').Length;

Note : this method will be listing in String class
**** extension methods should be static methods. The first parameter of the method should be the reference of the class we wish to extend refered by this operator.

Comments or Responses

Login to post response