Reading notes C # Advanced programming delegates and events

Source: Internet
Author: User
Tags define bool readline sort tostring
Notes | programming | advanced

When you use a class in C #, there are two phases. You first need to define this class by telling the compiler what fields and methods the class consists of. Then (unless you use only static methods) instantiate an object of the class. You also need to go through these two steps when using a delegate. First define the delegate to use, and for a delegate, define it by telling the compiler that the type represents that type of method, and then creating one or more instances of the delegate.
The definition of a delegate starts with delegate. But how does it work? Maybe a mouse event would be easier to understand, and here's the example from the book.

Using System;

Namespace Wrox.ProfCSharp.AdvancedCSharp
{
delegate bool CompareOp (object LHS, object RHS);

Class Mainentrypoint
{
static void Main ()
{
Employee [] Employees =
{
New Employee ("Karli Watson", 20000),
New Employee ("Bill Gates", 10000),
New Employee ("Simon Robinson", 25000),
New Employee ("Mortimer", (decimal) 1000000.38),
New Employee ("Arabel Jones", 23000),
New Employee ("Avon from ' Blake ' s 7 '", 50000)};
CompareOp Employeecompareop = new CompareOp (employee.rhsisgreater);
Bubblesorter.sort (employees, EMPLOYEECOMPAREOP);

for (int i=0; i<employees. Length; i++)
Console.WriteLine (Employees[i]. ToString ());
Console.ReadLine ();
}
}

Class Employee//: Object
{
private string name;
private decimal salary;

Public Employee (string name, decimal salary)
{
THIS.name = name;
This.salary = salary;
}

public override string ToString ()
{
return string. Format (name + ", {0:c}", salary);
}

public static bool Rhsisgreater (object LHS, Object RHS)
{
Employee EMPLHS = (employee) LHS;
Employee EMPRHS = (employee) RHS;
Return (Emprhs.salary > Emplhs.salary)? True:false;
}
}

Class Bubblesorter
{
static public void Sort (object [] Sortarray, CompareOp Gtmethod)
{
for (int i=0; i<sortarray.length; i++)
{
for (int j=i+1; j<sortarray.length; j + +)
{
if (Gtmethod (Sortarray[j], sortarray[i])
{
Object temp = Sortarray[i];
Sortarray[i] = Sortarray[j];
SORTARRAY[J] = temp;
}
}
}
}
}
}

Using System;

Namespace Wrox.ProfCSharp.AdvancedCSharp
{
delegate bool CompareOp (object LHS, object RHS);

Class Mainentrypoint
{
static void Main ()
{
Employee [] Employees =
{
New Employee ("Karli Watson", 20000),
New Employee ("Bill Gates", 10000),
New Employee ("Simon Robinson", 25000),
New Employee ("Mortimer", (decimal) 1000000.38),
New Employee ("Arabel Jones", 23000),
New Employee ("Avon from ' Blake ' s 7 '", 50000)};
CompareOp Employeecompareop = new CompareOp (employee.rhsisgreater);
Bubblesorter.sort (employees, EMPLOYEECOMPAREOP);

for (int i=0; i<employees. Length; i++)
Console.WriteLine (Employees[i]. ToString ());
Console.ReadLine ();
}
}

Class Employee//: Object
{
private string name;
private decimal salary;

Public Employee (string name, decimal salary)
{
THIS.name = name;
This.salary = salary;
}

public override string ToString ()
{
return string. Format (name + ", {0:c}", salary);
}

public static bool Rhsisgreater (object LHS, Object RHS)
{
Employee EMPLHS = (employee) LHS;
Employee EMPRHS = (employee) RHS;
Return (Emprhs.salary > Emplhs.salary)? True:false;
}
}

Class Bubblesorter
{
static public void Sort (object [] Sortarray, CompareOp Gtmethod)
{
for (int i=0; i<sortarray.length; i++)
{
for (int j=i+1; j<sortarray.length; j + +)
{
if (Gtmethod (Sortarray[j], sortarray[i])
{
Object temp = Sortarray[i];
Sortarray[i] = Sortarray[j];
SORTARRAY[J] = temp;
}
}
}
}
}
}

In the code, you first declare a delegate bool CompareOp (object LHS, Object RHS) delegate, and say the delegate:
The delegation mechanism is a kind of docking strategy that causes the event to be sent and the event accepts, the response of the object to the surrounding signal or to the notification behavior of other objects in a certain environment is described as the so-called "event", which can simulate the ability of the person to produce signal to the world around. A delegate is a directional signal flow: a technique that specifies the generation and reception of a signal and generates signal feedback. So how does this delegate move the program, look at the following code.
The first is compareop Employeecompareop = new CompareOp (employee.rhsisgreater) Here is like defining a listening device, once a CompareOp (object LHS, Object RHS) This event is going to execute the Employee.rhsisgreater code.
Next we'll go and see what's inside the employee.rhsisgreater.


public static bool Rhsisgreater (object LHS, Object RHS)
{
Employee EMPLHS = (employee) LHS;
Employee EMPRHS = (employee) RHS;
Return (Emprhs.salary > Emplhs.salary)? True:false;
}

public static bool Rhsisgreater (object LHS, Object RHS)
{
Employee EMPLHS = (employee) LHS;
Employee EMPRHS = (employee) RHS;
Return (Emprhs.salary > Emplhs.salary)? True:false;
}


That is to say, the parameters of Rhsisgreater are matched compareop exist, the parameter does not directly use the employee this type but uses a common practice, all make the object when needed to do the conversion, temporarily still can't understand its far-reaching significance, first remember. It is estimated that you cannot use these custom type when you define a delegate.
So when does this code run, pay attention to this paragraph


static public void Sort (object [] Sortarray, CompareOp gtmethod)
{
   for (int i=0; i<sortarray . Length; i++)
   {
        for (int j=i+1; j<sortarray.length; j + +)
        {
           if ( Gtmethod (Sortarray[j], sortarray[i])
           {
              Object temp = Sortarray[i];
              Sortarray[i] = sortarray[j];
              sortarray[j] = temp;
          }
       }
  }
}


Where the parameters of the static public void Sort (object [] Sortarray, CompareOp Gtmethod) have this kind of compareop we've commissioned. That is, once you run to the IF (Gtmethod (SORTARRAY[J), sortarray[i]) system, you will find compareop Employeecompareop = new CompareOp ( Employee.rhsisgreater), and then find the public static bool Rhsisgreater (object LHS, Object RHS) to execute the code inside.
The entire event response is complete.



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.