Some usages of base and this keywords in C#

In fact, the biggest use of base is in the polymorphism of relative development. Base can complete the creation of a derived class instance by calling its base class constructor or calling a method on the base class that has been overridden by other methods.
For example:


2.1 About base calling the base class constructor

public class A
{
        public A()
        {
                Console.WriteLine("Build A");
        }
}
public class B:A
{
        public B():base()
        {
                Console.WriteLine("Build B");
        }
        static void Main()
        {
                B b = new B();
                Console.ReadLine();
        }
}

Create an instance object of B, and the result is to print Build A and Build B at the same time.


2.2 About base The method of calling the base class in the derived class.

public class A
{
        public virtual void Hello()
        {
                Console.WiriteLine("Hello");
        }
}
public class B : A
{
        public override void Hello()
        {               
                base.Hello();//Call the method of the base class to display Hello
                Console.WiriteLine("World");
        }
}

In this way, if the program calls B.Hello(), the effect obtained will be Hello World.


Finally, according to the introduction of MSDN Library, these two keywords belong to the type of [access keyword]

 

++++++++++++++++++++++++++++ Invincible dividing line++++++++++++++++++ +++++++++++

++++++++++++++++++++++++++++++ Article 2++++++++++++++++++++ ++++++++++

 

about base

The base keyword is used to access members of the base class from derived classes:
to call methods on the base class that have been overridden by other methods.
Specifies the base class constructor that should be called when creating an instance of the derived class.
Base class access can only be done in constructors, instance methods, or instance property accessors.


Example:
1. Calling a base class method in a derived class.

using System;
public class BaseClass
{
    protected string _className = "BaseClass";
    public virtual void PrintName()
    {
        Console.WriteLine("Class Name: {0}", _className);
    }
}
class DerivedClass : BaseClass
{
    public string _className = "DerivedClass";
    public override void PrintName()
    {
        Console.Write("The BaseClass Name is {0}");
        //Call the base class method
        base.PrintName();
        Console.WriteLine("This DerivedClass is {0}", _className);
    }
}
class TestApp
{
    public static void Main()
    {
        DerivedClass dc = new DerivedClass();
        dc.PrintName();
    }
}

2. Call the base class constructor in the derived class.

// keywords_base2.cs
using System;
public class BaseClass
{
    int whether;
    public BaseClass()
    {
        Console.WriteLine("in BaseClass()");
    }
    public BaseClass(int i)
    {
        whether = i;
        Console.WriteLine("in BaseClass(int {0})", num);
    }
}
public class DerivedClass : BaseClass
{
    // The constructor calls BaseClass.BaseClass()
    public DerivedClass()
        : base()
    {
    }
    // The constructor calls BaseClass.BaseClass(int i)
    public DerivedClass(int i)
        : base(i)
    {
    }
    static void Main()
    {
        DerivedClass dc = new DerivedClass();
        DerivedClass dc1 = new DerivedClass(1)();
        Console.ReadLine();
    }
}

Note:
It is an error to use the base keyword from a static method.
base is mainly used for object-oriented development, which is reflected in example 2.

 

about this

The this keyword refers to the current instance of the class.
Common uses of this are:
Qualifying members hidden by similar names
Passing objects as arguments to other methods
Declaring indexers


Example: 

// this keyword
// keywords_this.cs
using System;
class Employee
{
    private string _name;
    private int _age;
    private string[] _arr = new string[5];
    public Employee(string name, int age)
    {
        // Use this to limit the fields, name and age
        this._name = name;
        this._age = age;
    }
    public string Name
    {
        get { return this._name; }
    }
    public int Age
    {
        get { return this._age; }
    }
    // print employee data
    public void PrintEmployee()
    {
        // Pass the Employee object as a parameter to the DoPrint method
        Print.DoPrint(this);
    }
    // declare indexer
    public string this[int param]
    {
        get { return _arr[param]; }
        set { _arr[param] = value; }
    }
}
class Print
{
    public static void DoPrint(Employee e)
    {
        Console.WriteLine("Name: {0}\nAge: {1}", e.Name, e.Age);
    }
}
class TestApp
{
    static void Main()
    {
        Employee E = new Employee("Hunts", 21);
        E[0] = "Scott";
        E[1] = "Leigh";
        E[4] = "Kiwis";
        E.PrintEmployee();
        for (int i = 0; i < 5; i++)
        {
            Console.WriteLine("Friends Name: {0}", E[i]);
        }
        Console.ReadLine();
    }
}

Guess you like

Origin blog.csdn.net/mr_five55/article/details/130897350