C # tips: The difference between new and override in inheritance

Source: Internet
Author: User

In inheritance of methods and attributes, the New Keyword indicates hiding the implementation of the base class, so that the subclass can also define methods and attributes with the same name as the base class.

The override keyword indicates rewriting. After the method or attribute of the base class is overwritten in the subclass, the method or attribute that calls the subclass will be executed from the subclass.

It may be very easy to say, but I will understand it at first glance.

Let's first look at the example of the New Keyword:

Basic controlmodel. CS

using System;namespace InhibitPropertyDemo{    public class ControlModel    {        private bool isChanged = true;        public bool IsChanged        {            get            {                return this.isChanged;            }            set            {                if (this.isChanged != value)                {                    this.isChanged = value;                    Console.WriteLine(string.Format("ControlModel::IsChanged = {0}", value));                }            }        }    }}

Subclass buttonmodel. CS

using System;namespace InhibitPropertyDemo{    public class ButtonModel : ControlModel    {        private bool isChanged = true;        public new bool IsChanged        {            get            {                return base.IsChanged || this.isChanged;            }            set            {                base.IsChanged = value;                if (this.isChanged != value)                {                    this.isChanged = value;                    Console.WriteLine(string.Format("ButtonModel::IsChanged = {0}", value));                }            }        }    }}

Test main program: program. CS

using System;namespace InhibitPropertyDemo{    class Program    {        static void Main(string[] args)        {            Console.WriteLine("==== ControlModel model = new ControlModel() ====");            ControlModel controlModel = new ControlModel();            controlModel.IsChanged = true;            controlModel.IsChanged = false;            Console.WriteLine(string.Empty);            Console.WriteLine("==== ButtonModel model = new ButtonModel() ====");            ButtonModel buttonModel = new ButtonModel();            buttonModel.IsChanged = true;            buttonModel.IsChanged = false;            Console.WriteLine(string.Empty);            Console.WriteLine("==== ControlModel model = new ButtonModel() ====");            ControlModel model = new ButtonModel();            model.IsChanged = true;            model.IsChanged = false;            Console.WriteLine(string.Empty);        }    }}

Execution result:

Let's take a look at the example of override:

Basic controlmodel. CS

using System;namespace InhibitPropertyDemo{    public class ControlModel    {        private bool isChanged = true;        public virtual bool IsChanged        {            get            {                return this.isChanged;            }            set            {                if (this.isChanged != value)                {                    this.isChanged = value;                    Console.WriteLine(string.Format("ControlModel::IsChanged = {0}", value));                }            }        }    }}

Subclass buttonmodel. CS

using System;namespace InhibitPropertyDemo{    public class ButtonModel : ControlModel    {        private bool isChanged = true;        public override bool IsChanged        {            get            {                return base.IsChanged || this.isChanged;            }            set            {                base.IsChanged = value;                if (this.isChanged != value)                {                    this.isChanged = value;                    Console.WriteLine(string.Format("ButtonModel::IsChanged = {0}", value));                }            }        }    }}

The main program. CS remains unchanged.

Execution result:

From the comparison of the results, we can see that if we want the instance of the subclass buttonmodel to always execute the methods and attributes of the subclass, we should use the override keyword in inheritance.

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.