C # delegate essential exploration VII. Pass Method variables as function parameters

Source: Internet
Author: User

// Pass method variables as function parameters, also called callback
Using System;
Using System. Collections;
Using System. Collections. Generic;
Using System. Text;

Namespace ConsoleApplication1.SeventhTest
{
Class SeventhTest
{
Public void DoTest ()
{
Lead Zhu Ge = new lead ();
Subordinate Wei = new subordinate ();

Zhu Ge. Add a task (Wei. His work, "Operation World of Warcraft ");
Zhu Ge. Processing the task ();

Console. ReadLine ();
Zhu Ge. Add tasks (Wei, his own job, "handling industry competition ");
Zhu Ge. Processing the task ();
}

}

Delegate string delegate task (string task content );

Class leader
{
Private Hashtable task list;

Add a public void task (delegate a task, string Task Type)
{
If (task list = null)
Task List = new Hashtable ();
Task list [Task Type] = one task;
}

Public void processing task ()
{
Foreach (DictionaryEntry task item in task list)
{
String result = (delegate task) task item. Value) (string) task item. Key );
Console. WriteLine (result );
}
}

}

Class subordinates
{
Public string work (string task content)
{
StringBuilder work content = new StringBuilder ();
If (Task content = "Operation World of Warcraft ")
{
Work content. AppendLine ("arrange device procurement ");
Work content. AppendLine ("Recruitment customer service, induction training ");
Work content. AppendLine ("advertising ");
Work content. AppendLine ("game market ");
Work content. AppendLine ("launch activity ");
Work content. AppendLine ("............ ");
}
Else if (Task content = "processing industry competition ")
{
Work content. AppendLine ("investigating competitors ");
Work content. AppendLine ("fight ");
}
Return work content. ToString ();
}

}

// In the previous example, the leader made public his own affairs, and his subordinates made further efforts to deal with the affairs disclosed by the leader.
// In this example, the leader receives and handles external tasks.
// One is open to the outside, and the other is receive internally.
}

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.