C # Study Notes (6) Delegation

Source: Internet
Author: User

Using System; using System. collections. generic; using System. componentModel; using System. data; using System. drawing; using System. linq; using System. text; using System. threading. tasks; using System. windows. forms; namespace demo6 {public partial class Form1: Form {public Form1 () {InitializeComponent ();} delegate string StartWorkDelegate (); // define the delegate StartWorkDelegate delegate void OffWorkDelegate (string s ); // Define the delegate OffWorkDelegate private void button1_Click (object sender, EventArgs e) // click the action executed by the go to work button {StartOffWork sw = new StartOffWork (); // instantiate the StartOffWork class/* instantiate the delegate StartWorkDelegate */StartWorkDelegate SWork = new StartWorkDelegate (sw. startWork); // StartWorkDelegate SWork = sw. startWork; // you can also use this statement to instantiate label2.Text = SWork (); // call the delegate and the result is displayed on the form} private void button2_Click (object sender, EventArgs e ){/* Instantiate the delegate OffWorkDelegate * // OffWorkDelegate OWork = new OffWorkDelegate (StartOffWork. offWork); OffWorkDelegate OWork = StartOffWork. offWork; // associate with the static method OWork (""); // call the delegate, the parameter is the end time} private void timer1_Tick_1 (object sender, EventArgs e) // Tick Event code of timer1 {label1.Text = "current time:" + DateTime. now. toString () ;}} public class StartOffWork // defines the management class StartOffWork {public string StartWork () // instance method {if (D AteTime. Now <= Convert. ToDateTime ("20:30") // you can determine if you have already gone to work {return "very early! ";}// The returned string is" very early !" Else {return "You are late. You can come early next time! ";}} Public static void OffWork (string offworktime) // static method {if (DateTime. now> Convert. toDateTime (offworktime) // determines whether the end time is {MessageBox. show ("hard work! ") ;}// The pop-up message box else {MessageBox. Show (" it's not time to get off work! ");}}}}

Related Article

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.