What really is the purpose of “base” keyword in c#?

后端 未结 9 1188
误落风尘
误落风尘 2020-12-08 00:42

Thus for used base class for some commom reusable methods in every page of my application...

public class BaseClass:System.Web.UI.Page
{
   public string Get         


        
9条回答
  •  旧时难觅i
    2020-12-08 01:01

    The base keyword is used to access members in the base class that have been overridden (or hidden) by members in the subclass.

    For example:

    public class Foo
    {
        public virtual void Baz()
        {
            Console.WriteLine("Foo.Baz");
        }
    }
    
    public class Bar : Foo
    {
        public override void Baz()
        {
            Console.WriteLine("Bar.Baz");
        }
    
        public override void Test()
        {
            base.Baz();
            Baz();
        }
    }
    

    Calling Bar.Test would then output:

    Foo.Baz;
    Bar.Baz;
    

提交回复
热议问题