Virtual VS Override VS new Keyword in ASP.Net C#

Virtual vs Override vs new Keyword in ASP.Net C#

Hello everyone, I am going o share the basic oops concept over the virtual, override and new keywords. The brief about it with example as given bellows.

What Is virtual keyword?
The virtual keyword is use to modify a method,  property, indexer, or event declared in the base class and It is allows to be overridden in the derived class.

What Is override keyword?
The override keyword is use to modify a virtual or abstract method, property, indexer, or event of the base class into the derived class.

What Is new keyword?
The new keyword is used to hide a method, property, indexer, or event of the base class into the derived class.
The Examples as given below.








Inherited Methods
namespace ConsoleNamespace
{
    class ClassA
    {
        public void MethodA()
        {
            Console.WriteLine("A::Foo()");
        }
    }

    class ClassB : ClassA { }

    class Test
    {
        static void Main(string[] args)
        {
            ClassA a = new ClassA();
            a.MethodA(); 
            // output --> "A::MethodA()"

            ClassB b = new ClassB();
            b.MethodA(); 
            // output --> "A::MethodA()"
        }
    }
}

The method MethodA() can be overridden in classes ClassB and ClassC

//The method MethodA() can be overridden in classes ClassB and ClassC
namespace ConsoleNamespace
{
    class ClassA
    {
        public void MethodA()
        {
            Console.WriteLine("ClassA::MethodA()");
        }
    }

    class ClassB : ClassA
    {
        public void MethodA()
        {
            Console.WriteLine("ClassB:MethodA()");
        }
    }

    class Test
    {
        static void Main(string[] args)
        {
            ClassA a = new ClassA();
            ClassB b = new ClassB();
            a.MethodA();
            // output --> "ClassA::MethodA()"

            b.MethodA();
            // output --> "ClassB::MethodA()"

            a = new ClassB();
            a.MethodA();
            // output --> "ClassA::MethodA()"
        }
    }
}


Virtual and Overridden Methods

namespace ConsoleNamespace
{
    class ClassA
    {
        public virtual void MethodA()
        {
            Console.WriteLine("ClassA::MethodA()");
        }
    }

    class ClassB : ClassA
    {
        public override void MethodA()
        {
            Console.WriteLine("ClassB:MethodA()");
        }
    }

    class Test
    {
        static void Main(string[] args)
        {
            ClassA a = new ClassA();
            ClassB b = new ClassB();
            a.MethodA();
            // output --> "ClassA::MethodA()"

            b.MethodA();
            // output --> "ClassB::MethodA()"

            a = new ClassB();
            a.MethodA();
            // output --> "ClassB::MethodA()"

            Console.ReadLine();
        }
    }
}


Method Hiding

namespace ConsoleNamespace
{
    class ClassA
    {
        public virtual void MethodA()
        {
            Console.WriteLine("ClassA::MethodA()");
        }
    }

    class ClassB : ClassA
    {
        public new void MethodA()
        {
            Console.WriteLine("ClassB:MethodA()");
        }
    }

    class Test
    {
        static void Main(string[] args)
        {
            ClassA a = new ClassA();
            ClassB b = new ClassB();
            a.MethodA();
            // output --> "ClassA::MethodA()"

            b.MethodA();
            // output --> "ClassB::MethodA()"

            a = new ClassB();
            a.MethodA();
            // output --> "ClassB::MethodA()"

            Console.ReadLine();
        }
    }
}

Combining Method Overriding and Hiding

namespace ConsoleNamespace
{
    class ClassA
    {
        public void MethodA()
        {
            Console.WriteLine("ClassA::MethodA()");
        }
    }

    class ClassB : ClassA
    {
        public virtual new void MethodA()
        {
            Console.WriteLine("ClassB:MethodA()");
        }
    }

    class Test
    {
        static void Main(string[] args)
        {
            ClassA a = new ClassA();
            ClassB b = new ClassB();
            a.MethodA();
            // output --> "ClassA::MethodA()"

            b.MethodA();
            // output --> "ClassB::MethodA()"

            a = new ClassB();
            a.MethodA();
            // output --> "ClassB::MethodA()"

            Console.ReadLine();
        }
    }
}



ANIL SINGH

Hey! I'm Anil Singh. I author this blog. I'm Active Blogger, Programmer. I love learning new technologies, programming, blogging and participating the forum discussions more...
My Blogs - http://www.code-sample.com and http://www.code-sample.xyz
My Books - Buy Books Online at Best Prices

You Might Also Like
Post a Comment
www.code-sample.com/. Powered by Blogger.