Ad Code

Responsive Advertisement

Extension Method

  Extension Method :

   1. In our first rule we have discussed that execution of a child class always starts by invoking default  constructor of its parent but in any of the situation if parent class contain parameterized constructor in  place of default then child class is not capable of implicitly calling parameterized constructor of parent.

To test this rewrite the constructor of parent class Class1 as following-

 Public Class1(int x)
  {
   Console.WriteLine("class1 constructor :" + x);
  }

Now try to execute child class Class2, where you will be getting a compilation error stating Class1 doesn’t contain default constructor in it. To resolve the above problem now from child class constructor we should explicitly invoke the constructor of Class1 by supplying the required parameters to execute, using the keyword ‘base’.

To check this rewrites the constructor of Class2 as following:

Public Class2(int x, int y ):base (x)
 { 
  Console.WriteLine("class2 constructor:" + y);
 }

In above case the keyword “base” invokes the base class constructor by supplying the value to the parameter, which should be passed by creating object of class2 from main.
     Class2 c = new Class2(100,200);

# Consuming a Class from other Classes –

A class can be consumed from other classes in two different ways –
       1. Inheriting and consuming.
       2. Creating the object and consuming.
     
    To check the Second process adds a new class Class3.cs and writes the following-
class Class3
 {
  static void Main()
   { 
    Class1 obj = new Class1(100);
    obj.test1(); obj.test2();
    Console.ReadLine();
   }
 }

Extension Method –

  1. It is a new feature added in C#3.0, which allows adding new method under a class without editing its       source code.

        2. The concept is somewhat similar to constant of inheritance, which can be used for extending the             functionalities of existing class. Whereas in inheritance after extending the functionalities of a class         we can access the old and new method by using object of child class in which we extended the               functionalities.
In the above case using extension methods we were defining new method under Class2 and the binding them with Class1. Here the advantage is we can call both old and new methods using object of Class1, but in inheritance we need to call by using object of child class Class2.

Defining Extension Method -


    1. Extension method can be defined only under static class.
     2. As extension method are being define in static class, we need to define the Main static Method but          once we are bound to any class will become instance method, which should called using object of the    class.
     3. The first parameter of an extension method should be the name of the class to which the method            should belong, prefixed with “this” keyword where this parameter is not considered by invoking the          method.
     4. We can pass any required parameters to in extension method, but that should be specified from the        second place while defining the method.
     5. One Extension method can be bound only with one class.
     6. Under a single static class any number of extension method can be defined bound with the different        classes.

-Add a class Original.cs and write the following :

class Original
{
 public int x = 500;
 public void Show1()
 {
  Console.WriteLine("First method :"+ this.x);
 }
 public void Show2()
 {
  Console.WriteLine("Secand Method");
 }
}

-Add a class StatClass.cs and write the following :

static class StatClass
 {
  public static void Show3(this Original obj)
  {
   Console.WriteLine("Third method");
  }
  public static void Show4(this Original obj, int x)
  {
   Console.WriteLine("Fourth method:"+ x);
  }
  public static void Show5(this Original obj)
  {
   Console.WriteLine("Fifth method:"+ obj.x);
  }
 }
-Add a class testOrigiinal.cs and write the following :

class TestOriginal
 {
  static void Main()
  { 
   Original obj = new Original();
   obj.Show1(); obj.Show2(); obj.Show3();
   obj.Show4(100); obj.Show5();
   Console.ReadLine();
  }
 }
Reactions

Post a Comment

0 Comments