The base and this keywords in C #

Source: Internet
Author: User

Base:
It is used to implement access to the public or protected members of the base class in a derived class, but is limited to constructors, instance methods, and instance attribute accessors.
The specific features summarized in msdn include:
(1) Call methods that have been overwritten by other methods on the base class.
(2) Specify the base class constructor to call when creating a derived class instance.
Base is often used to communicate with the base class when the object of the derived class is initialized.
The base can access the public and protected members of the base class. Private Members cannot access the base class.
In multi-level inheritance, the base can point to the parent class methods in two cases: first, if there is an overload, the Base will point to the method of directly inherited parent class members; without overload, the base can point to the public or protected methods of any parent class.
This:
It is used to reference the current instance of the class, and also includes the inherited methods. This can usually be hidden.
The summary functions in msdn mainly include:

(1) restrict hidden members with similar names
(2) passing objects as parameters to other methods
(3) Declare the Indexer
This refers to the class object itself, used to access all constants, fields, attributes, and method members of the class, regardless of the access element at any access level. Because this is only confined to the inside of the object and cannot be seen outside the object, this is the basic idea of this. In addition, static members are not part of objects, so they cannot be referenced in static methods.

General rules:

1. Use less or use less base and this. In addition to avoiding subclass name conflicts and calling other constructors in a constructor, the use of base and this may cause unnecessary results.
2. Using base and this in static members is not allowed. The reason is that base and this access are class instances, that is, objects, while static members can only be accessed by classes and cannot be accessed by objects.
3. Base is designed to achieve polymorphism.
4. You can use the this or base keyword to specify only one constructor. That is to say, you cannot simultaneously apply this and base to one constructor.
5. In simple terms, base is used to access the override base class members in the derived class. This is used to access the members of this class, and of course includes the inherited public and protected members.
6. In addition to base, the other way to access base class members is to convert the displayed type. This method cannot be a static method.

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.