Thursday, June 2, 2016

Why we write base.somemthod()


 Why we write base.somemthod() in Child method.


Reson that we want to perform some task related task to complete the whole activity. first we excuted our cutom logic behavoiur in our Child class ovveriden method now to performwhole related activity We called base class same mthod that we have override in child class.

References Check out the Filters. First derived your custom filter ovveride method and see in overiden mthod its calling base.samemthod()

namespace ConsoleApplication1
{
    //Case 1 Class has same interface method that Interface.
    public class AuthorizeAttribute
    {
        string restwork = "My base work";
        public virtual void OnAuthorization(string filterContext) //If method is virtual then you you must declare iots body..
        {
            Console.WriteLine("Hi");
        }
        public void OnAuthorization1(string filterContext)
        {
            Console.WriteLine("Hi");
        }
    }
    public interface IInterface
    {
        void OnAuthorization(String filterContext);
        void OnAuthorization1(String filterContext);
    }
    class Class1 : AuthorizeAttribute, IInterface //We dont need to impliment interface mthod since Class1 ultimateli got implimented method from his base class AuthorizeAttribute
        //this class allready implimented same method.
    {
    }

    public class AuthorizeAttribute1
    {
       public static string Work = "My base work";
        public static int Work1 = 0;
        public virtual void OnAuthorization(int resttask) //If method is virtual then you you must declare iots body..
        {
            Work1 = Work1 + resttask;
        }
        public void OnAuthorization1(string filterContext)
        {
            Console.WriteLine("Hi");
        }
    }

    public class CustomAuthorizeAttribute: AuthorizeAttribute1
    {
        public CustomAuthorizeAttribute()
        {
            Work1 = 2;
        }
        public override void OnAuthorization(int currenttask)
        {
            Work1 = Work1 + currenttask + 2;
            base.OnAuthorization(Work1);
        }
        public void FinalOutCome()
        {
            Console.WriteLine("The Final outcomeof base class variable ovveride in child then perform base task is" + Work1);
        }
    }

    public class MyClass1
    {
        public static void Main()
        {
            CustomAuthorizeAttribute c = new CustomAuthorizeAttribute();
            c.OnAuthorization(2);
            c.FinalOutCome();
            Console.ReadLine();
        }
    }

}
The Final outcomeof base class variable ovveride in child then perform base task
 is12

No comments:

Post a Comment