Understanding the base and this keywords through instances

Source: Internet
Author: User

/// <Summary> /// from the parainfo class, let me understand the difference between base and this. Obviously, base is used for inheritance, this can be used to call the series constructor. // </Summary> [serializable] public class parainfo {public string name {Get; set;} public object value {Get; set ;} public String evaluate {Get; set;} Public String paraname {Get; set;} public parameterdirection direction {Get; set;} public parainfo () {This. condition = "="; this. paraname = This. name; this. direction = parameterdirection. input;} public parainfo (string name, object Val): this () {This. name = Name; this. value = val;} public parainfo (string name, object Val, string values): This (name, Val) {This. signature = signature;} public parainfo (string name, object Val, string delimiter, string paraname): This (name, Val, struct) {This. paraname = paraname;} public parainfo (string name, object Val, string separator, string paraname, parameterdirection direction): This (name, Val, delimiter, paraname) {This. direction = direction;} public parainfo this [int I] {get {return this [I] ;}set {This [I] = value ;}}}

As can be seen from the instance code, this keyword has three main functions:

1. Use this to eliminate ambiguity. This keyword provides access to the current class instance.

2. Use this to call the series constructor. This design mode is useful when the class defines multiple constructors.

Members of base access base class

This referenceCurrent object

 

Base keyword

The base keyword accesses the base class members from the derived class, including:

  • 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

Access to base classes can only be performed in constructors, instance methods, or instance attribute accessors. It is incorrect to use the base keyword in a static method.

Example 1

// Keywords_base.cs
// Accessing base class members
Using system;
Public class person
{
Protected string SSN = "444-55-6666 ";
Protected string name = "John L. malgraine ";

Public Virtual void getinfo ()
{
Console. writeline ("Name: {0}", name );
Console. writeline ("SSN: {0}", SSN );
}
}
Class EMPLOYEE: person
{
Public String id = "abc567efg ";
Public override void getinfo ()
{
// Calling the base class getinfo method:
Base. getinfo ();
Console. writeline ("employee ID: {0}", ID );
}
}

Class testclass
{
Static void main ()
{
Employee E = new employee ();
E. getinfo ();
}
}
The result is as follows:
In baseclass ()
In baseclass (int I)

 

This keyword

1,This refers to the current object itself.

This keyword should be added to a class to explicitly indicate the use of the object's own variables or functions.

2,Pass this as a parameter

You can also use this when you want to pass yourself as a parameter to another object. For example:

Using system; public class A {public a () // 2. class B is constructed in Class A and passed as a parameter. After the parameter is complete, execute 3. then 4. {New B (this ). print ();} public void print () {console. writeline ("hello from! ") ;}} Public class B {A; Public B (A) {This. A = A; // assign a value to variable A in Class B} public void print () {. print (); // call print () of Class A to output hello from a console. writeline ("hello from B! "); // Then output hello from B! } Class program {public static void main (string [] ARGs) {A x = new A (); // 1. Execute class a construction that is 2 .}}

 

Running result:

Hello from!

Hello from B!

This example describes a type of "has a": B has a A, that is, B's instance has a member variable of the type. In addition to "has a", "is a" and "like a" are the most common class relationships.

 

 

This keyword references the current instance of the method to be called. The static member function does not have the this pointer. This keyword can be used to access members in constructors, instance methods, and instance accessors.

The following is the general purpose of this:

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

Variables declared in static methods, static property accessors, or fieldsIntializer)It is wrong to reference this in

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.