C Basic Grammar: Base Keyword Learning Notes

  • 2021-06-28 13:42:01
  • OfStack

Like this keyword 1, it exists abbreviated or replaced as an instance of a class (and therefore cannot call static and abstract members of a base class). However, the this keyword is used to replace instances of this class, and the base keyword is used to replace instances of a base class. The usage is simple. It accesses the base class as follows:

base. [Identifier]

base [[Expression List]] This type of 1 gives you a rough idea of the indexer operation most often used for base class instances, as you'll see in the code I'm demonstrating below.

For base. [Identifier], explain again under 1:

For non-virtual methods, this access is only a direct access to the base class instance members, which is exactly equivalent to ((base)this). [identifier].

For a virtual method, overriding the virtual method for this visiting subclass uses this form of access, which is also a direct access to the base class instance members (the mechanism for virtual method invocation is disabled) and treats it as a non-virtual method, which is not equivalent to ((base)this). [Identifier], because this format fully follows the mechanism for virtual method invocation and declares that it is an accumulative type at the time of trial.The runtime is a subclass type, so the override method of the subclass is also executed.Unoverridden virtual methods are equivalent to simple non-virtual method processing.

The test code is as follows:


using System;

namespace BaseTest
{
  class father
  {
    string str1 = "this field[1] of baseclass", str2 = "this field[2] of baseclass";
    public void F1() //Non-virtual method
    {
      Console.WriteLine(" F1 of the baseclass");
    }
    public virtual void F2()//virtual method
    {
      Console.WriteLine(" F2 of the baseclass");
    }
    public virtual void F3()
    {
      Console.WriteLine(" F3 of the baseclass that is not overrided "); 
    }
    public string this[int index]
    {
      set
      {
        if (index==1 )
        {
          str1 = value;
        }
        else
        {
          str2 = value;
        }
      }
      get
      {
        if (index ==1)
        {
          return str1;
        }
        else
        {
          return str2;
        }
      }
    }
  }
  class Child:father
  {
    public void G()
    {
      Console.WriteLine("======Non-virtual methods Test =========");
      base.F1();
      ((father)this).F1();
      Console.WriteLine("======virtual methods Test=========");
      base.F2();
      ((father)this).F2();
      base.F3();
      ((father)this).F3();
      Console.WriteLine("=====Test the type that the tbase [[expression]] ==========");
      Console.WriteLine(base[1]);
      base[1] = "override the default ";
      Console.WriteLine(base[1]);
      Console.WriteLine("================Test Over=====================");
    }
    public override void F2()
    {
      Console.WriteLine(" F2 of the subclass ");
    }
    
    static void Main(string[] args)
    {
      Child child=new Child();
      child.G();
      Console.ReadKey();
    }
  }
}


base is used for constructor declarations, and this is used for constructor declarations that are exactly the same, but base is a match to the base class constructor parameters.



using System;

namespace BaseCoTest
{
  class Base
  {
    public Base(int a, string str)
    {
      Console.WriteLine("Base. Base(int a,string str)");
    }
    public Base(int a)
    {
      Console.WriteLine("Base. Base(int a)");
    }
    public Base()
    {
    }
  }
  class Sub : Base
  {
    public Sub()
    {
    }
    public Sub(int a)
      : base(1, "123")
    {
      Console.WriteLine("Sub .Sub(int a)");
    }
    class Test
    {
      public static void Main()
      {
        Sub sub = new Sub(1);
        Console.ReadKey();
      }
    }
  }
}


Related articles: