Keywords: This and base

Source: Internet
Author: User

This and base are classified as access keywords, which are used to implement access operations on the Inheritance Mechanism.

This is used to reference the current instance of the class, or to reference the inherited method.

Main functions of this:

• Restrict hidden members with similar names
• Passing objects as parameters to other methods
• Declare the Indexer
• This refers to the class object itself, which is used to access all constants, fields, attributes, and method members of the class.
• This is only confined to the inside of an object and cannot be seen outside the object. This is the basic idea of this.
* This appears in the constructor of the class as a value type, which indicates reference to the object itself being constructed

* This appears in the class method as a value type, indicating reference to the object that calls this method

* This appears in the constructor of the structure as a variable type, indicating a reference to the structure being constructed

* This is a variable type in the structure method, indicating a reference to the structure that calls this method.

Base is used to access the public or protected members in the base class from the derived class, but is limited to constructors, instance methods, and instance attribute accessors.

Base features:

• 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
• Base is often used to communicate with the base class during object initialization of the derived class.
• Base can access accumulated public and protected members, while private members cannot access
• In multi-level inheritance, the base can direct to the parent class in two ways:
• When there is a overload, base points to the method of the parent class that is directly inherited.
• Without overloading, the base can point to the public or protected methods of any parent class.

General rules

• Use less or use less this and base
• The use of base and this in static members is not allowed, because base and this access are class instances, that is, objects, and static members can only be accessed by classes, cannot be accessed by objects.
• Base is designed to achieve Polymorphism
• You can use the this or base keyword to specify only one constructor. The base and this cannot be used on the same function at the same time.
• Base is used to access the rewritten base class members in the derived class, And this is used to access the members of this class, including the inherited public or protected members.
• In addition to base, another method for accessing base class members is: display type conversion, but this method cannot be a static method.

 

Msdn reference

 

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.