The understanding of virtual (virtual method) in C # and the difference from abstract method

Source: Internet
Author: User

Virtual method (virtual method)

The virtual keyword is used to decorate a method in a base class. There are two things you can do with virtual:

Scenario 1: The virtual method is defined in the base class, but the virtual method is not overridden in the derived class. In a call to a derived class instance, the virtual method uses the method defined by the base class.

Scenario 2: The virtual method is defined in the base class and then overridden in a derived class by using override. In a call to a derived class instance, the virtual method uses the derived overridden method.

The code is as follows:
Namespace Virtualtestapplication
{
public class A
{
Public virtual string print ()
{
Return "I am a class";
}

}

public class B:a
{

}

public class C:a
{
public override string Print ()
{
Return "I am Class C";
}
}
}

Then invoke the method in the main program Portal:
static void Main (string[] args)
{
b b = new B ();
c C = new C ();
Console.WriteLine (B.print ());
Console.WriteLine (C.print ());
Console.readkey ();
}
Operation Result:

The similarities and differences between virtual and abstract:

Both virtual and abstract are used to decorate the parent class by overriding the definition of the parent class to redefine the subclass.

They have one thing in common: if you are using a method, you must add public to the front, or you will get a compilation error: a virtual or abstract method cannot be private. After all, adding virtual or abstract is to redefine the subclass, and private members cannot be accessed by the quilt class.

Different points:

1, the method of virtual modification must be implemented (even if only a pair of curly braces), and the method of the abstract modification must not be implemented. If the method for the virtual modification is not implemented:

        public class Test1        {public            virtual void fun1 ();        }

Error: "TEST1.FUN1 ()" must declare the principal because it is not marked as abstract, extern, or partial

For the method of the abstract modification, if it is implemented:

        Public abstract class Test2        {public            abstract void Fun2 () {}        }

Error: "Test2.fun2 ()" Cannot declare the principal because it is marked abstract

2, virtual can quilt class rewrite, and abstract must quilt class rewrite,

    Class BaseTest1    {public       virtual void fun () {}//must have implementation    }    class Derivetest1:basetest1    {        // public override void Fun () {}    }

The compilation will not have an error, if you override the virtual decoration method, you must add override (this tells the compiler you want to override the virtual method), and must be implemented, or compile error;

    Abstract class BaseTest2    {public        abstract void Fun ();    }    Class Derivetest2:basetest2    {        //public override void Fun (); Error 1: No implementation        //public  void Fun () {}  Error 2: Override did not add override        //override void Fun () {} Error 3: Virtual member or abstract member cannot be private (as long as the virtual member or abstract member is declared in the parent class, even if the inheritance is added with this restriction)        public override void Fun () {}//correctly overrides the abstract method;       }

3. If a class member is abstract, the class must be abstract before it, because only abstract classes can have abstract methods.

4, cannot create an instance of the abstract class, can only be inherited cannot be instantiated, such as: BaseTest2 base2 = new BaseTest2 (); A compilation error will occur: An abstract class or interface cannot create an instance.
5. In C # If you want to override a method in a subclass, you must add virtual to the parent class method before the subclass method, adding override before the child class, thus avoiding the programmer accidentally overriding the parent class method in the subclass.

# # # #我只是刚跨入C # Industry of small white, but also everywhere looking for information, many are copied and pasted. If there is any shortage of places, also ask you to raise. Give some advice to the niche.

The understanding of virtual (virtual method) in C # and the difference from abstract method

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.