C # Reference Access keywords: base, this_c# tutorial

Source: Internet
Author: User
Tags constructor
Base

The base keyword is used to access the members of the base class from a derived class:

Invokes a method on the base class that has been overridden by another method.
Specifies the base class constructor that should be called when creating instances of the derived class.
Base class access can only be done in constructors, instance methods, or instance property accessors.

Example:

Call the base class method in the derived class.
Base keyword
Accessing base class Members
Using System;

public class BaseClass
{
protected string _classname = "BaseClass";

public virtual void Printname ()
{
Console.WriteLine ("Class Name: {0}", _classname);
}
}

Class Derivedclass:baseclass
{
public string _classname = "DerivedClass";

public override void Printname ()
{
Console.Write ("The BaseClass Name is {0}");
Call base class method
Base. Printname ();
Console.WriteLine ("This derivedclass is {0}", _classname);
}
}

Class TESTAPP
{
public static void Main ()
{
DerivedClass dc = new DerivedClass ();
dc. Printname ();
}
}

/**//*
Console output:
The BaseClass Name is BaseClass
This derivedclass is DerivedClass
*/
Call the base class constructor in a derived class.
Keywords_base2.cs
Using System;
public class BaseClass
{
int num;

Public BaseClass ()
{
Console.WriteLine ("In BaseClass ()");
}

public BaseClass (int i)
{
num = i;
Console.WriteLine ("In BaseClass (int {0})", num);
}
}

public class Derivedclass:baseclass
{
The constructor calls the Baseclass.baseclass ()
Public DerivedClass (): Base ()
{
}

The constructor calls Baseclass.baseclass (int i)
Public derivedclass (int i): base (i)
{
}

static void Main ()
{
DerivedClass dc = new DerivedClass ();
DerivedClass DC1 = new DerivedClass (1);
}
}

/**//*
Console output:
In BaseClass ()
In BaseClass (1)
*/

Pay attention.

It is an error to use the base keyword from a static method.
Base is primarily used for object-oriented development, and is reflected in example 2.
This
The This keyword refers to the current instance of the class.

The following are common uses for this:

Qualify members that are hidden by similar names
Passing an object as a parameter to another method
Declaring indexers
Example:

Comprehensive example.
this keyword
Keywords_this.cs
Using System;
Class Employee
{
private string _name;
private int _age;
Private string[] _arr = new STRING[5];

Public Employee (string name, int age)
{
Use this to qualify fields, name and age
This._name = name;
This._age = age;
}

public string Name
{
get {return this._name;}
}

public int Age
{
get {return this._age;}
}

Print employee information
public void Printemployee ()
{
Passing the employee object as a parameter to the Doprint method
Print.doprint (this);
}

Declaring indexers
public string this[int param]
{
get {return _arr[param];}
set {_arr[param] = value;}
}

}
Class Print
{
public static void Doprint (Employee e)
{
Console.WriteLine ("Name: {0}\nage: {1}", E.name, E.age);
}
}

Class TESTAPP
{
static void Main ()
{
Employee E = new Employee ("hunts", 21);
E[0] = "Scott";
E[1] = "Leigh";
E[4] = "Kiwis";
E.printemployee ();

for (int i=0; i<5; i++)
{
Console.WriteLine ("Friends Name: {0}", E[i]);
}

Console.ReadLine ();
}
}

/**//*
Console output:
Name:hunts
Age:21
Friends Name:scott
Friends Name:leigh
Friends Name:
Friends Name:
Friends Name:kiwis
*/

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.