The difference between c#base and this

Source: Internet
Author: User

Objective:

Today I just learn C # friend suddenly asked me how to use the base, suddenly asked me to live, think of me when I learned C # Foundation when the basic is a run-off, very regret, so hurriedly Baidu, wrote this blog record down.

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.
The specific features of the MSDN summary include:
(1) Call a method on the base class that has been overridden by another method.
(2) Specifies 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, and also to include inherited methods, which can usually be hidden.
The summary features in MSDN include:

(1) Qualifying members that are hidden by similar names
(2) Passing objects as parameters to other methods
(3) Declaring indexers
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 access 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. Use this or the base keyword to specify only one constructor, which means that this and base cannot be combined on a single constructor.
5, in simple terms, base is used to access the overridden base class members in derived classes, and this is used to access members of this class, including, of course, inherited public and protected members.
6. In addition to base, another way to access a base class member is to implement the type conversion shown. Only the method cannot be a static method.

Speech:

1. The foundation is not strong, learning is not high, want to write code, there are some outstanding achievements, in addition to Qinxuekulian, solid foundation is essential.

2. I will book a C # Basic learning program in the near future, estimated for one months.

The difference between c#base and this

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.