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

后端 未结 9 1189
误落风尘
误落风尘 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条回答
  •  不知归路
    2020-12-08 01:09

    The base keyword is used to refer to the base class when chaining constructors or when you want to access a member (method, property, anything) in the base class that has been overridden or hidden in the current class. For example,

    class A {
        protected virtual void Foo() {
            Console.WriteLine("I'm A");
        }
    }
    
    class B : A {
        protected override void Foo() {
            Console.WriteLine("I'm B");
        }
    
        public void Bar() {
            Foo();
            base.Foo();
        }
    }
    

    With these definitions,

    new B().Bar();
    

    would output

    I'm B
    I'm A
    

提交回复
热议问题