C # Grammar Learning: This&&base

Source: Internet
Author: User
Tags constructor modifier

The This keyword refers to the current instance of the Class (Class) (Instance) and is also used as a modifier (Modifier) for the first parameter of the Curried method.

The following are common uses for this:

This keyword is used where:

1, Index

2,this, simply, represents the class. To be precise is to represent the class of the image.

3, other occasions. Represents a constructor

//要限定被類似名稱所隱藏的成員,例如:
public Employee(string name, string alias)
{
this.name = name;
this.alias = alias;
}
//要將物件做為參數傳遞到其他方法,例如:
CalcTax(this);

//要宣告索引子,例如:
public int this [int param]
{
get { return array[param]; }
set { array[param] = value; }
}
//因為靜態成員函式存在於類別層級,且非物件的一部分,所以不具有 this 指標。在靜態方法中參考 this 是錯誤的。

The base keyword is used to access the base class (base class) member in the derived class:

Call a method in the base category that has been overridden by another method.

Specifies the base class constructor to call when creating a derived class instance.

The base class is only allowed to be accessed in a constructor, an instance of the execution method (Instance methods), or an instance of an individual property accessor.

It is an error to use the base keyword in a static method.

//In this case, both the base type person and the derived category employee have a method called Getinfo. Using the//base keyword, you can call the Getinfo method of the base type from the derivative.
//Accessing base class members
using System:
public class person
{
protected string ssn = "444-55-66 66 ";
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 ();
}
}

//This example shows how to specify a base-type constructor for a call when creating a derived type of execute.
using System;
public class BaseClass
{
int num;
Public baseclass ()
{
Console.WriteLine (' In BaseClass () ');
}
Public BaseClass (int i)
{
num = I
Console.WriteLine ("In BaseClass (int i)");
}
Public int getnum ()
{
return num;
}
}
public class Derivedclass:baseclass
{
//This constructor'll call Baseclass.baseclass ()
Public DerivedClass (): Base ()
{
}
//This constructor'll call Baseclass.baseclass (int i)
Public derivedclass (i NT i): Base (i)
{
}
static void Main ()
{
DerivedClass md = new DerivedClass ();
DerivedClass md1 = new DerivedClass (1);
}
}

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.