Tags

, , ,


Extension Methods

Extension method is an implementation of Decorator Structural Pattern. The ToolTip control is a closer technical match to the Decorator, but extension method are logically close enough.
Extension methods are designed to support adding behaviors that look like they belong to an existing class when you can’t add behaviours. For example you can’t inherit and add behaviors to intrinsic type like int and string.
Extension methods clean up the clumsy syntax of wrapper methods and permit you to extend sealed classes and intrinsic types. Some rules are
• Use extension methods to extend sealed types without inheritance.
• Use extension method to keep deep inheritance hierarchies from getting out of hand.
• Extension methods are static methods in static class
• The first argument of an extension method must use the this modifier preceding the argument type; this refers to the  type being extended.
• Extension properties, events, and operators are not supported.
• Extension methods are less discoverable and more limited the instance methods
• Extension methods are supported by intelligence.
• Generic extension methods are supported
• Extension methods are not really members, so we can only access public members of the object being extended.
• An extension method implicitly use the ExtensionAttribute

 Implementing Extension Methods

Implementing Extension Method for intrinsic type (int).

Ex 1:

namespace MyLinq.Extension
{
    public class MyExtension
    {
        public static void Main(string[] args)
        {
            12.Show(); //
            Console.Read();
        }
    }
    //Extension Method exmple
    public static  class MyExtension
    {
        public static void Show(this int intNum)
        {
            Console.WriteLine(intNum);               
        }
    }
}

Ex 2:

namespace MyLinq.Extension
{
    public class MyExtension
    {
        public static void Main(string[] args)
        {
             “Jitendra Kumar”.Show();
            Console.Read();
        }
    }
    //Extension Method exmple
    public static  class MyExtension
    {
        public static void Show(this string strString)
        {
            Console.WriteLine(strString);               
        }
    }
}
 

Implementing Extension Method Overloading for intrinsic types.

namespace MyLinq.Extension
{
    public class MyExtension
    {
        public static void Main(string[] args)
        {
            12.Show(); //    
             “Jitendra Kumar”.Show();
            Console.Read();
        }
    }
    //Extension Method exmple
    public static  class MyExtension
    {
        public static void Show(this int intNum)
        {
            Console.WriteLine(intNum);               
        }
        public static void Show(this string strString)
        {
            Console.WriteLine(strString);               
        }
    }
}

Implementing Extension Method with Parameter for intrinsic type.

namespace MyLinq.Extension
{
    public class MyExtension
    {
        public static void Main(string[] args)
        {
            12.Add(20); //    
             “Jitendra”.Add(“Kumar”);
            Console.Read();
        }
    }
    //Extension Method exmple
    public static  class MyExtension
    {
        public static void Add(this int intNum, int intValue)
        {
            Console.WriteLine(intNum+intValue);               
        }
        public static void Add(this string strString,string strValue)
        {
            Console.WriteLine(“{0) {1}”,strString,strValue);               
        }
    }
}

Implementing Extension Method with Return Value

namespace MyLinq.Extension
{
    public class MyExtension
    {
        public static void Main(string[] args)
        {
            Console.WriteLine(12.Add(20));     
            Console.Read();
        }
    }
    //Extension Method exmple
    public static  class MyExtension
    {
        public static int Add(this int intNum, int intValue)
        {
            return intNum+intValue;               
        }
    }
}

Implementing Extension Methods for Object
Ex1:

namespace MyLinq.Extension
{
    public class MyExtension
    {
        public static void Main(string[] args)
        {
            Person p=new Person();
            p.EmpId = 10;
            p.EmpName = “Pawan”;
            p.EmpAge = 30;
            p.Show();
            p.Show();
            Console.Read();
        }
    }
    sealed public class Person
    {
        private int empID;
        private string empName;
        public int EmpId
        {
            get { return empID; }
            set { empID = value; }
        }
        public string EmpName
        {
            get { return empName; }
            set { empName = value; }
        }
        //Auto implimented properties
        public int EmpAge { get; set; }
        ////Read Only Auto implimented properties
        //public int EmpAge { get; private set; }
        ////Write Only Auto implimented properties
        //public int EmpAge { get; private set; }
    }

    //Extension Method exmple
    public static  class MyExtension
    {
        public static void Show(this object o)
        {
            var t = o.GetType();
            PropertyInfo[] info = t.GetProperties();
            foreach(var p in info)
            {
                try
                {
                    Console.WriteLine(“{0}: {1}”, p.Name, p.GetValue(o, null));   
                }
                catch (Exception)
                {

                    Console.WriteLine(“{0}: {1}”, p.Name, “Unk.”);   
                }
               
            }
        }
    }
}

Ex2:

namespace MyLinq.Extension
{
    public class MyExtension
    {
        public static void Main(string[] args)
        {
            Person p=new Person();
            p.EmpId = 10;
            p.EmpName = “Pawan”;
            p.EmpAge = 30;
            p.Show();
            p.Show();
            Console.Read();
        }
    }
    sealed public class Person
    {
        private int empID;
        private string empName;
        public int EmpId
        {
            get { return empID; }
            set { empID = value; }
        }
        public string EmpName
        {
            get { return empName; }
            set { empName = value; }
        }
        //Auto implimented properties
        public int EmpAge { get; set; }
        ////Read Only Auto implimented properties
        //public int EmpAge { get; private set; }
        ////Write Only Auto implimented properties
        //public int EmpAge { get; private set; }
    }

    //Extension Method exmple
    public static  class MyExtension
    {
        public static void Show(this object o)
        {
 Person p=(Person) o;
 Console.WriteLine(“Emp Id : {0}, Emp Name : {1} Emp Age : {2}”,p.EmpId,p.EmpName,p.EmpAge)
        }
    }
}

Implementing Extension Method and pass a list of Objects

namespace MyLinq.Extension
{
    public class MyExtension
    {
        public static void Main(string[] args)
        {
            List<Person> pList=new List<Person>();
            pList.Add(new Person() { EmpId = 1, EmpName = “One”, EmpAge = 10 });
            pList.Add(new Person() { EmpId = 1, EmpName = “One”, EmpAge = 10 });
            pList.Show();
           Console.Read();
        }
    }
    sealed public class Person
    {
        private int empID;
        private string empName;
        public int EmpId
        {
            get { return empID; }
            set { empID = value; }
        }
        public string EmpName
        {
            get { return empName; }
            set { empName = value; }
        }
        //Auto implimented properties
        public int EmpAge { get; set; }
        ////Read Only Auto implimented properties
        //public int EmpAge { get; private set; }
        ////Write Only Auto implimented properties
        //public int EmpAge { get; private set; }
    }

    //Extension Method exmple
    public static  class MyExtension
    {
       public static void Show(this IList<Person> list)
       {
            foreach(var p in list)
            {
                p.Show();
            }
       }

       public static void Show(this object o)
       {
            var t = o.GetType();
            PropertyInfo[] info = t.GetProperties();
            foreach(var p in info)
            {
                try
                {
                    Console.WriteLine(“{0}: {1}”, p.Name, p.GetValue(o, null));   
                }
                catch (Exception)
                {

                    Console.WriteLine(“{0}: {1}”, p.Name, “Unk.”);   
                }
               
            }
       }
      
//      public static void Show(this object o)
//      {
//        Person p=(Person) o;
//        Console.WriteLine(“Emp Id : {0}, Emp Name : {1} Emp Age : {2}”,p.EmpId,p.EmpName,p.EmpAge)
//      }

    }
}

 

Extension Function Support LINQ

namespace MyLinq.Extension
{
    public class MyExtension
    {
        public static void Main(string[] args)
        {
            var numbers = new int[] {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20};
            Func<int, bool> getEvent = delegate(int num)
                                           {
                                               return num%2==0;
                                           };
            IEnumerable<int> events=numbers.Where<int>(getEvent);
            IEnumerator<int> enumerators = events.GetEnumerator();
            while(enumerators.MoveNext())
            {
                Console.WriteLine(enumerators.Current);
            }
            Console.Read();
        }
    }
}

Advertisements