Skip to main content

Posts

Showing posts with the label Extension Methods

Extension Methods - add methods to class

Consider the following class   public class Person     {         public string FirstName { get ; set ; }         private string LastName { get ; set ; }         protected string Email { get ; set ; }         public int Age { get ; set ; }        virtual public void showName()         {             Console .WriteLine( "base class" );         }     } To add additional methods to a class you have to subclass it or add the function directly to the class. If the class is sealed then you will have very limited options only. In C#, you can use the new extension method feature to add a new method to an existing type. To add the method to the existing cla...