C # Explanation of the keywords base and this,

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.