C # Explanation of the keywords base and this,
Base this
Base
Official Description: keywords are used to access members of the base class from the derived class:
Call methods that have been overwritten by other methods on the base class.
Specify the base class constructor to call when creating a derived class instance.
Only base class access is allowed in constructors, instance methods, or instance attribute accessors.
This
Official Description: it refers to the current instance of the class and can also be used as the modifier of the first parameter of the extension method.
Project: LearnBaseAndThis
Environment VS2017. NET Core2.0
Inherit the call Method
Class Father {public Father () {Console. writeLine ($ "{nameof (Father )}. constructor ");} public Father (string name) {Console. writeLine ($ "{nameof (Father )}. constructor, Name = {name} ");} public virtual void DoSomething () {Console. writeLine ($ "{nameof (Father )}. work ") ;}} class Son: Father // inherit from Father {public Son (): this (" radish ") {Console. writeLine ($ "{nameof (Son )}. constructor ");} public Son (string name) {Console. writeLine ($ "{nameof (Son )}. constructor, Name = {name} ");} public override void DoSomething () {Console. writeLine ($ "{nameof (Son )}. learn ") ;}} class Daughter: Father // inherit from Father {public Daughter (): base (" ") {Console. writeLine ($ "{nameof (Son )}. constructor ");} public override void DoSomething () {Console. writeLine ($ "{nameof (Daughter )}. play ") ;}}// output content static void Main (string [] args) {Father f = new Father (); f. doSomething (); Console. writeLine ("------------------------"); Son s = new Son (); s. doSomething (); Console. writeLine ("------------------------"); Daughter d = new Daughter (); d. doSomething (); Console. readKey ();} outputs the following content: Father. constructor Father. work ------------------------ Father. constructor Son. constructor, Name = radish Son. constructor Son. learn ------------------------ Father. constructor, Name = radish Son. constructor Daughter. play
The Son class expresses this usage. During construction, the default constructor of the base class is called by default, and then you can check whether you have specified this constructor.
The Daughter class represents the base usage. When constructing a base class, you must first determine whether to call a construction method of the base class. If not, the default structure is called.
This extension method
Public static class Extension {public static double ToDollar (this double I) {return ToDollar (I, 6);} public static double ToDollar (this double I, int j) {return I * = j ;}} static void Main (string [] args) {double j = 2; Console. writeLine (j. toDollar (7 ). toString (); // output 14 Console. writeLine (j. toDollar (). toString (); // OUTPUT 12 Console. readKey ();}
In this example, the double class is extended to the ToDollar (converted to the dollar) method, and parameters are set according to the extension requirements.
Official explanation of the extension method: the extension method enables you to "add" a method to an existing type without creating a new derived type, re-compiling, or modifying the original type in other ways. An extension method is a special static method, but it can be called like an instance method of an extension type. For client code written in C #, F #, and Visual Basic, there is no significant difference between calling an extension method and calling a method actually defined in the type.
You can use the extension method to query a set. By referencing System. LINQ, you will find that there are many more query methods such as where and count.
Notes
Base and this cannot be used in static methods. This is also often used in the current class to use this. Attribute or this. Method to indicate that you want to call the current attribute and method. It is best to put the extension methods together to facilitate management and maintenance.