Skip to main content

Posts

Showing posts from August, 2017

Extension methods in C#

Consider the class C# 1 2 3 4 5 6 7 8 9 10 11 12 13          namespace ExtensionMethod      {          public class testClass {              public string sayHello ( ) {              return "Hello" ;            }        }      }     Invoke the above from your form using C# 1 2 3 4 5 6          testClass test = new testClass ( ) ;      MessageBox . Show ( test . sayHello ( ) ) ;     This will show “Hello” in message box. Consider the scenario where you don’t have the access to the above code and you want to add another method to the above class.  That is where the C# feature of extension method is to be used. Extension methods Extension methods as the name implies, is about extending the functionalities of the class.  The Extension methods help you add a new method to the existing Class (or Type). Now let u