The base and this keyword in C #

Source: Internet
Author: User

Base: Used to implement access to a base class public or protected member in a derived class, but only in constructors, instance methods, and instance property accessors. Specific features of the MSDN Summary include: (1call a method on the base class that has been overridden by another method. (2Specifies the base class constructor that should be called when creating an instance of a derived class. Base is commonly used to communicate with the base class when the derived class object is initialized. Base can access the public and protected members of the base class, and private members are inaccessible. In multi-tier inheritance, there are two ways that a base can point to a parent class: First, if there is an overload, base points to a method that inherits directly from the parent class member, and if there is no overload, base can point to either the public or protected method of any parent class.  This: Used to refer to the current instance of a class, including inherited methods, which can often be hidden. The summary features in MSDN include the following: (1) to qualify members that are hidden by similar names (2) to pass the object as a parameter to another method (3declares the indexer this refers to the class object itself, which accesses all constants, fields, properties, and method members of this class, regardless of the access level of the element. Because this is only confined to the inside of the object, the outside of the object is not visible, this is the basic idea of this. In addition, a static member is not part of an object and therefore cannot be referenced in a static method. General rules:1, try to use less or no base and this. In addition to avoiding name collisions of subclasses and calling other constructors in one constructor, the use of base and this can cause unnecessary results. 2, using base and this in static members is not allowed. The reason is that both base and this access are instances of the class, that is, objects, and static members can only be accessed by the class and cannot be accessed by the object. 3, Base is designed to achieve polymorphism. 4, you can specify only one constructor using the this or base keyword, which means that this and base cannot be combined on a single constructor. 5In simple terms, base is used to access the overridden base class members in a derived class, and this is used to access members of this class, including, of course, the inherited public and protected members. 6, in addition to base, another way to access the base class members is to display the type conversions to implement. Only the method cannot be a static method.

The base and this keyword in C #

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.