This article is a mirror article of machine translation, please click here to jump to the original article.

View: 17906|Reply: 0

[Source] base and this keywords in C#

[Copy link]
Posted on 6/27/2016 11:33:30 PM | | |

base:
Used to enable access to public or protected members of the base class in derived classes, but only in constructors, instance methods, and instance property accessors.
Specific features of the summary in MSDN include:
    (1) Call a method that has been overwritten by other methods on the base class.
    (2) Specify the base class constructor that should be called when creating a derived class instance.
base is commonly used to communicate with the base class when the derived class object is initialized.
base can access public and protected members of the base class, and private members are not accessible.
In multi-layer inheritance, there are two situations where base can point to the parent class: first, when there is an overload, base will point to the method of the parent class member that directly inherits; In the absence of overload, base can point to a public or protected method of any parent parent.
this:
The current instance used to reference the class, also inherited methods, can usually hide this.
The summary features in MSDN mainly include:

    (1) Limit members who are hidden by similar names
    (2) Pass the object as a parameter to other methods
    (3) Declaration indexer
This refers to the class object itself, which is used to access all constants, fields, attributes, and method members of the class, regardless of the access level of the access element. Because this is only limited to the inside of the object, and cannot be seen outside the object, this is the basic idea of this. Also, static members are not part of the object, so this cannot be referenced in static methods.

General rules:

1. Use base and this as little as possible. In addition to avoiding subclass name conflicts and calling other constructors in one constructor, the use of base and this can lead to unwanted results.
2. The use of base and this in static members is not allowed. The reason is that base and this access both instances of classes, i.e., objects, while static members can only be accessed by classes, not objects.
3. Base is designed to achieve polymorphism.
4. Use the this or base keywords to specify only one constructor, which means that this and base cannot be applied to a constructor at the same time.
5. In simple terms, base is used to access the rewritten base class members in the derived class; This is used to access members of this category, of course, including inherited public ownership and protected members.
6. In addition to base, another way to access base class members is to convert the displayed type. It's just that the method can't be a static method.




Previous:asp.net 404 error when uploading large files in mvc
Next:What is .NET Core?
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com