[. Net you must know] (6): go deep into the keywords-base and this

Source: Internet
Author: User
Tags mscorlib
[. Net you must know] (6): go deep into the keywords --- base and this

Author: anytao

 

 

This article introduces the following:

    • Basic object-oriented concepts
    • Basic keywords
    • This keyword is easy to understand.

 

1. Introduction

The new keyword has attracted a lot of attention, especially thanks to Anders Liu. It makes me feel that the communication platform provided by the blog Park is truly ubiquitous. Therefore, it is necessary for us to continue with this topic and carry out the keywords that I think are the most noteworthy. The focus of this article is the access key words: Base and this. Although keyword access is not a difficult topic to understand, we can clarify our ideas in depth. It's still the old method. I will first list the problem and check whether you are ready.

    1. Can I use base and this in static methods? Why?
    2. What aspects of base are commonly used? What are the common features of this?
    3. Can base access all members of the base class?
    4. If there are three layers or more inheritance, what is the base of the lower-level derived class pointing to that layer? For example, in the. NET system, if the base is used for access, should it be a direct parent class instance or a top-level class instance?
    5. What is the execution sequence of inheriting Class Object Instantiation when base and this are applied to constructors?

2. Basic Concepts

Base and this are attributed to access keywords in C #. As the name suggests, they are used to implement access operations of the Inheritance Mechanism to satisfy access to object members, this provides a more flexible processing method for the polymorphism mechanism.

2.1 base keywords

It is used to implement access to the public or protected members of the base class in the derived class, but is limited to constructors, instance methods, and instance attribute accessors, the specific features summarized in msdn include:

    • 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.

2.2 this keyword

It is used to reference the current instance of the class, and also includes inherited methods. This can usually be hidden. The summary function in msdn mainly includes:

    • Restrict hidden members with similar names
    • Passing objects as parameters to other methods
    • Declare Indexer

3. easy to understand

3.1.

Below is a small example to illustrate the application of base and this in access operations, so as to have a brief understanding of it. For more detailed rules and further details, we will go on to elaborate. This example does not have a complete design concept. It is mainly used to describe the key points and difficulties of using the base and this keywords. The details are as follows:

Base and this example
Using system;

Namespace anytao.net. my_must_net
{
Public class action
{
Public static void Torun (vehicle Vehicle)
{
Console. writeline ("{0} is running.", vehicle. tostring ());
}
}

Public class Vehicle
{
Private string name;
Private int speed;
Private string [] array = new string [10];

Public Vehicle ()
{
}

// Restrict hidden members by similar names
Public Vehicle (string name, int speed)
{
This. Name = Name;
This. speed = speed;
}

Public Virtual void showresult ()
{
Console. writeline ("the top speed of {0} is {1}.", name, speed );
}

Public void run ()
{
// Pass the parameters of the current instance
Action. Torun (this );
}

// Declare the indexer, which must be this, so that the object can be indexed like an array
Public String This [int Param]
{
Get {return array [Param];}
Set {array [Param] = value ;}
}
}

Public class car: Vehicle
{
// The derived class communicates with the base class. The base class is called first.
// Specify the base class constructor to call when creating a derived class instance
Public Car ()
: Base ("car", 200)
{}

Public Car (string name, int speed)
: This ()
{}

Public override void showresult ()
{
// Call a method that has been overwritten by other methods on the base class
Base. showresult ();
Console. writeline ("it's a car's result .");
}

}

Public class Audi: Car
{
Public Audi ()
: Base ("Audi", 300)
{}

Public Audi (string name, int speed)
: This ()
{
}

Public override void showresult ()
{
// It can be seen from layer-3 inheritance that base can only inherit its direct base class members
Base. showresult ();
Base. Run ();
Console. writeline ("It's Audi's result .");
}
}

public class basethistester
{< br> Public static void main (string [] ARGs)
{< br> Audi = New Audi ();
Audi [1] = "A6";
Audi [2] = "A8";
console. writeline (Audi [1]);
Audi. run ();
Audi. showresult ();
}< BR >}

3.2 Example

The above example shows all the basic functions used by base and this. The specific descriptions can be explained in the annotations. The following descriptions further describe and supplement the annotations, to illustrate the main points of application:

    1. Base is often used to communicate with the base class when the object of the derived class is initialized.
    2. The base can access the public and protected members of the base class. Private Members cannot access the base class.
    3. 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.
    4. In multi-level inheritance, the base can point to the parent class methods in two situations: first, if there is an overload, the Base will point to the method of directly inherited parent class members, for example, in the showresult method of the Audi class, the base is used to access the car. showresult () method, but cannot access vehicle. the showresult () method. Instead, the base can point to the public or protected method of any parent class. For example, in the Audi class, you can use the base class to access the vehicle. run () method. In this case, we can use ildasm.exeCode.
. Method public hidebysig virtual instance void
Showresult () cel managed
{
// Code size 27 (0x1b)
. Maxstack 8
Il_0000: NOP
Il_0001: ldarg.0
// Base calls the parent class member
Il_0002: Call instance void anytao.net. my_must_net.car: showresult ()
Il_0007: NOP
Il_0008: ldarg.0
// Base calls the parent class member. Because car. Run () is not implemented, it points to a more advanced parent class.
Il_0009: Call instance void anytao.net. my_must_net.vehicle: Run ()
Il_000e: NOP
Il_000f: ldstr "It's Audi's result ."
Il_0014: Call void [mscorlib] system. Console: writeline (string)
Il_0019: NOP
Il_001a: Ret
} // End of method Audi: showresult

3.3 in-depth analysis

If there are three or more inheritance, what is the base of the lower-level derived class pointing? For example, in the. NET system, if the base is used for access, should it be a direct parent class instance or a top-level class instance?

First, we need to know the instantiation sequence during the class creation process to further understand the detailed execution process of the base mechanism. In general, the instantiation process first needs to instantiate its base class, and so on until the system. object is instantiated. Therefore, class instantiation always starts when system. Object. Object () is called. Therefore, the instantiation process of the class Audi in the example can be summarized as follows. For details, refer to the sample code analysis.

    1. Run system. Object. Object ();
    2. Run vehicle. Vehicle (string name, int speed );
    3. Run car. Car ();
    4. Run car. Car (string name, int speed );
    5. Run audi. Audi ();
    6. Run audi. Audi (string name, int speed ).

On the basis of fully understanding the instantiation sequence, we can smoothly grasp the implementation of base and this when acting on the constructor, and further understand the basic function details.

The more important analysis below is to analyze the Il decompilation code based on the ildasm.exe tool, so that we can have a deeper understanding of the application essence behind the base and this. Only in this way can we have a basic analysis of the technology.

The execution of the main method is as follows:

Il Analysis Base and this execution
. Method public hidebysig static void main (string [] ARGs) cel managed
{
. Entrypoint
// Code size 61 (0x3d)
. Maxstack 3
. Locals Init (class anytao.net. my_must_net.audi V_0)
Il_0000: NOP
// Use the newobj command to create a new object and call the constructor for initialization.
Il_0001: newobj instance void anytao.net. my_must_net.audi:. ctor ()
Il_0006: stloc.0
Il_0007: ldloc.0
Il_0008: LDC. i4.1
Il_0009: ldstr "A6"
Il_000e: callvirt instance void anytao.net. my_must_net.vehicle: set_item (int32,
String)
Il_0013: NOP
Il_0014: ldloc.0
Il_0015: LDC. i4.2
Il_0016: ldstr "A8"
Il_001b: callvirt instance void anytao.net. my_must_net.vehicle: set_item (int32,
String)
Il_0020: NOP
Il_0021: ldloc.0
Il_0022: LDC. i4.1
Il_0023: callvirt instance string anytao.net. my_must_net.vehicle: get_item (int32)
Il_0028: Call void [mscorlib] system. Console: writeline (string)
Il_002d: NOP
Il_002e: ldloc.0
Il_002f: callvirt instance void anytao.net. my_must_net.vehicle: Run ()
Il_0034: NOP
Il_0035: ldloc.0
// Base. showresult finally calls the vehicle method of the highest level parent class,
// Instead of the direct parent class car. showresult () method, this should be followed
Il_0036: callvirt instance void anytao.net. my_must_net.vehicle: showresult ()
Il_003b: NOP
Il_003c: Ret
} // End of method basethistester: Main

Therefore, override the parent class method directs to the method Member of the highest level parent class.

4. General rules

    • Use less or use less base and this. In addition to determining subclass name conflicts and calling other constructors in a constructor, the use of base and this may cause unnecessary results.
    • 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.
    • Base is designed to achieve polymorphism.
    • Only one constructor can be specified using the this or base keyword. That is to say, this and base cannot be applied to one constructor at the same time.
    • To put it simply, base is used to access the rewritten base class members in the derived class. This is used to access the members of this class, including the inherited public and protected members.
    • In addition to base, the other way to access base class members is: display type conversion. This method cannot be a static method.

5. Conclusion

The base and this keywords are not particularly difficult to understand. In this article, the topic of this series should be summarized in addition to its application rules, more importantly, on the basis of paying attention to its implementation details, we should establish a clearer grasp and Analysis of the language background. These are the fundamental elements of learning and technology applications. net technical framework. For learners, only by grasping the concept in essence can they find the answer at a glance in the application of extraordinary changes.

Let's get down to the truth. I wonder if the readers have their own answers to the first few questions. We may wish to speak freely and have a deeper discussion to unveil the truth.

 

References

(USA) Stanley B. Lippman, C # Primer

(USA) David Chappell, understanding. net

(Cnblog) bear-study-hard, C # learning notes (2): execution sequence of Constructor

All in one

[Notice]

In addition, in the previous topic discussions, no matter the type or keyword, it involves the reference type and value type topics. I will discuss the relevant content in the near future, including three aspects, this is a recent trend in this series. I will make an advertisement for myself. Have a good time.

[Statement]

the keyword in this article refers to the keyword concept in C #, not in the general sense. net CRL category, the reason why this topic is added to this series is based on. which of the following is the key point for us to develop a basic language under the. NET system. Therefore, we do not need to investigate what is. Net or C #. I hope you can clarify the concept and put it bluntly.

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.