C#学习笔记(8)——委托应用(显示,写入时间)

说明(2017-5-30 09:08:10):

1. 定义一个委托,public delegate void MyDel();无参数,无返回值。

2. 委托作为DoSth的参数,DoSth里面调用委托。

3. 写两个方法ShowTime和WriteTime。

4. main函数里面,DoSth参数是谁,就调用谁(ShowTime和WriteTime)。

5. 注意System.DateTime.Now.ToString(),和File类的使用。

代码:

 1 using System;
 2 using System.Collections.Generic;
 3 using System.IO;
 4 using System.Linq;
 5 using System.Text;
 6 using System.Threading.Tasks;
 7 
 8 namespace _05委托改变字符串
 9 {
10     class Program
11     {
12         public delegate void MyDel();
13         static void Main(string[] args)
14         {
15             DoSth(WriteTime);
16             Console.ReadKey();
17         }
18         public static void DoSth(MyDel mdl)
19         {
20             Console.WriteLine("吃早饭!");
21             Console.WriteLine("吃午饭!");
22             mdl();
23             Console.WriteLine("吃晚饭!");
24             Console.WriteLine("吃宵夜!");
25         }
26         public static void ShowTime()
27         {
28             Console.WriteLine(System.DateTime.Now.ToString());
29         }
30         public static void WriteTime()
31         {
32             File.WriteAllText("log.txt", System.DateTime.Now.ToString());
33         }
34     }
35 }
原文地址:https://www.cnblogs.com/Jacklovely/p/6919731.html