Tuesday, March 19, 2013

Extension Methods in C#.net


Extension Methods: 
-It is one of the best approach to extend the functionality of a class other than inheritance.

-Using this approach u can add new methods under an existing class even if u don't have permission to make changes on the class or the source code of the class is not available.

-After adding new methods to a class using the extension methods approach we can access those methods with the object of the class to which the methods were added, where as in inheritance they can be accessed only with the object of the child class.

-To define extension methods follow the below guidelines:
   i) Extension methods need to be defined only under a static class.
  ii) As they were being defined under a static class they must be declared as static but after linking with a class they will be considered as instance methods but not static for the class.
 iii) The first parameter of an extension method should be the name of the class to which the method belongs which will not be considered while calling.
  iv) If u want any parameters to an extension method we can pass them while defining but only from the second parameter.
   v) You can define any no of extension methods under a single static class linking with any no of classes.

No comments:

Post a Comment

amazon

Sukanya Samriddhi Account - SBI

SUKANYA SAMRIDDHI Account information by SBI SUKANYA SAMRIDDHI ACCOUNT : FACILITY AVAILABLE AT ALL BRANCHES OF SBI Sukanya Samriddhi ...