Mastery of C # Delegate and event (11.1-11.2)

Source: Internet
Author: User

1. The indexer method structure is roughly <modifier><return type> this [argument list], which can be defined in the interface: when declaring an indexer for an interface, remember that the declaration simply represents the presence of the indexer. You just need to provide the appropriate access function, not including the scope modifier. The following code declares the indexer as part of the interface Iimplementme:

Interface Iimplementme { string This[int Index] { get; Set   } 

The corresponding implemented class must specifically define the get and set access functions for the Iimplementme indexer.

An indexer can be called an attribute with parameters, which differs from a property in that the property name cannot be the same, cannot be overloaded, and the indexer can. The property can be static, but the indexer must be instantiated. In my opinion, the function of an indexer is that it can change the index of some existing indexers, such as an array so that the method is to query the corresponding data with an int as subscript, but I can rewrite the index so that the index of the array is in string.

Cases:

Namespace Study
{
Class Program
{
static void Main (string[] args)
{
Scoreindex s = new Scoreindex ();
s["Zhang San", 1] = 90;
s["Zhang San", 2] = 100;
s["Zhang San", 3] = 80;
s["John Doe", 1] = 60;
s["John Doe", 2] = 70;
s["John Doe", 3] = 50;
Console.WriteLine ("Zhang San Course number 1:" + s["Zhang San", 1]);
Console.WriteLine ("All achievements of the Zhang San are:");
ArrayList temp;
temp = s["Zhang San"];
foreach (Indexclass b in temp)
{
Console.WriteLine ("Name:" + b.name + "Course Number:" + B.courseid + "score:" + B.score);
}
Console.readkey ();
}
}
Class Indexclass
{
private string _name;
private int _courseid;
private int _score;
Public Indexclass (string _name, int _courseid, int _score)
{
This._name = _name;
This._courseid = _courseid;
This._score = _score;
}
public string Name
{
get {return _name;}
set {this._name = value;}
}
public int CourseID
{
get {return _courseid;}
set {This._courseid = value;}
}
public int Score
{
get {return _score;}
set {This._score = value;}
}
}
Class Scoreindex
{
Private ArrayList arr;
Public Scoreindex ()
{
arr = new ArrayList ();
}
public int this[string _name, int _courseid]
{
Get
{
foreach (Indexclass A in arr)
{
if (A.name = = _name && A.courseid = = _courseid)
{
return a.score;
}
}
return-1;
}
Set
{
Arr. ADD (New Indexclass (_name, _courseid, value)); arr["Zhang San", 1]=90
}
}
Overloaded indexers
Public ArrayList this[string _name]
{
Get
{
ArrayList temp = new ArrayList ();
foreach (Indexclass b in arr)
{
if (b.name = = _name)
{
Temp. ADD (b);
}
}
return temp;
}
}
}
}

2. Operator Overloading:

Operator overloading is the function of some existing operators, such as the addition of two classes, subtraction, comparison, etc., it provides the OPERAOR keyword overload, must be used in conjunction with the static keyword. Cases:

Namespace Containerandoperator
{
<summary>
Indexer, which is already a container class, and most of the indexers are used in container classes
The use of IEnumerable and GetEnumerator () is intended to enable the class to be foreach
</summary>
public class Containerclass:ienumerable
{
Private ArrayList people = new ArrayList ();
Public person This[int Index]//person is the value type returned, int is the type of the input index value, this is used to define the indexer
{
get {return (person) People[index];}
set {people. Insert (index, value); }
}

Public IEnumerator GetEnumerator ()
{
return people. GetEnumerator ();
}
}


public class Person
{
public string name {get; set;}
public string Id {get; set;}
public int Age {get; set;}
<summary>
Refactoring two-dollar operator
</summary>
<param name= "P1" ></param>
<param name= "P2" ></param>
<returns></returns>
public static person operator + (person P1,person p2)
{
return new person (P1.name + "-" + P2.name, p1. Id + "-" + P2. Id, p1. Age + P2. Age);
}
public static string operator-(person P1, person p2)
{
return p1.name+ "-" +p2. Id;
}
Public person () {}
Public person (string n,string i,int a)
{
name = N;
Id = i;
age = A;
}
}
}

Namespace Containerandoperator
{
Class Program
{
static void Main (string[] args)
{
Containerclass con = new Containerclass ();
Con[0] = new Person ("Homer", "11", 11);
CON[1] = new Person ("Homer1", "12", 12);
CON[2] = new Person ("Homer2", "13", 13);
Console.WriteLine (Con[2].name);
foreach (person s in con)
{
Console.WriteLine (S.name);
}
Console.WriteLine ("*******************************");
Person P3 = con[1] + con[2];
Console.WriteLine (P3.name);
Console.WriteLine (Con[1]-con[2]);
Console.ReadLine ();
}
}
}

Because sometimes we may need to compare two classes, we have done some specific functions, but we can overload the operators if the primitive operators are not supported.

Mastery of C # Delegate and event (11.1-11.2)

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.