The difference between new and override in C #

Source: Internet
Author: User

  • override means "overwrite", which means that the subclass overrides the parent class's method. The object of the subclass can no longer access the method in the parent class. (Signature must be the same)
  • new means "hidden", which means that the subclass hides the method of the parent class, and of course, the method of the parent class can be accessed in the object of the subclass through certain transformations.
  • What is the result of running the following code?

    [CSharp]View Plaincopyprint?
    1. Class Base
    2. {
    3. public virtual void F1 ()
    4. {
    5. Console.WriteLine ("Base s virtual function F1");
    6. }
    7. public virtual void F2 ()
    8. {
    9. Console.WriteLine ("Base ' s virtual fucntion F2");
    10. }
    11. }
    12. Class Derived:base
    13. {
    14. public override void F1 ()
    15. {
    16. Console.WriteLine ("Derived ' s override function F1");
    17. }
    18. public new void F2 ()
    19. {
    20. Console.WriteLine ("Derived ' s new function F2");
    21. }
    22. }
    23. Class Program
    24. {
    25. public static void Main (string[] args)
    26. {
    27. Base B1 = new Derived ();
    28. //Because the subclass overrides the method of the parent class, this is called the F1 method of the subclass. is also the embodiment of the multi-state in Oo
    29. B1. F1 ();
    30. //Because the method of the parent class is hidden in the subclass with new, so this is called the hidden Parent class method
    31. B1. F2 ();
    32. }
    33. }
    or we can use the following code to make it easier to understand:[CSharp]View Plaincopyprint?
    1. Class Program
    2. {
    3. public static void Main (string[] args)
    4. {
    5. Derived B1 = new Derived ();
    6. //Because the subclass overrides the method of the parent class, this is called the F1 method of the subclass. is also the embodiment of the multi-state in Oo
    7. (Base) B1). F1 ();
    8. //Because the method of the parent class is hidden in the subclass with new, so this is called the hidden Parent class method
    9. (Base) B1). F2 ();
    10. }
    11. }

    The above two outputs are:

    Derived ' s override function F1

    Base ' s virtual fucntion F2

    In the above example, because F1 overrides the original method, the F1 method of the subclass is still called even if the object is turned into the parent class. Because the F2 method of the subclass only "hides" the F2 method of the parent class, the F2 method of the previously hidden parent class is called when the object that is cast to the parent class (Base) calls the F2 method.

The difference between new and override in C #

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.