The difference between C# base and this

Foreword:

Today, a friend of mine who just learned C# suddenly asked me how to use base, and suddenly stopped me. I remembered that when I learned C# foundation, I was basically anticlimactic. I regret it very much, so I quickly googled it and wrote this blog. record it.

base:
Used to implement access to public or protected members of the base class in derived classes, but only in constructors, instance methods, and instance property accessors.
The specific functions summarized in MSDN include:
    (1) Calling methods on the base class that have been overridden by other methods. 
    (2) Specify the base class constructor that should be called when creating an instance of the derived class.
base is often used to communicate with the base class when the derived class object is initialized. 
base can access public and protected members of the base class, private members are inaccessible.
In multi-level inheritance, there are two cases for the methods of the parent class that base can point to: one is that when there is overloading, base will point to the method of the directly inherited parent class member; and when there is no overloading, base can point to any public or protected method of the parent class. 
this:
used to refer to the current instance of the class, including inherited methods, usually this can be hidden.
The summary functions in MSDN mainly include:

    (1) Qualify members that are hidden by similar names 
    (2) pass objects as parameters to other methods 
    (3) declare the indexer 
this refers to the class object itself and is used to access all constants, fields, properties, and method members of this class , and regardless of the access level of the access element. Because, this is only limited to the inside of the object, and the outside of the object cannot be seen. This is the basic idea of ​​this. Also, static members are not part of the object, so this cannot be referenced in static methods.

General rules:

1. Use as little or no base and this as possible. In addition to avoiding name collisions in subclasses and calling other constructors in one constructor, the use of base and this can easily lead to unwanted results. 
2. Both base and this are not allowed in static members. The reason is that both base and this access instances of classes, that is, objects, while static members can only be accessed by classes, not by objects. 
3, base is designed to achieve polymorphism. 
4. Only one constructor can be specified using the this or base keyword, that is to say, this and base cannot be applied to a constructor at the same time. 
5. In simple terms, base is used to access overridden base class members in derived classes; this is used to access members of this class, including inherited public and protected members. 
6. In addition to base, another way to access base class members is to implement explicit type conversion. It's just that the method cannot be a static method.

Testimonials:

1. The foundation is not strong, and the knowledge is not high. If you want to have some outstanding achievements in writing code, in addition to studying and practicing hard, a solid foundation is essential.

2. I will book a C# basic learning plan in the near future, which is expected to be a month.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325090892&siteId=291194637