代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
//在此可以定义变量吗?
delegate void TimeDelegate(string s);//委托类似于类吧,如果在此定义变量的话可以吗?是不是这里只能定义类、委托或者结构呢?还有没有其他呢?
class Mytime //没有指定命名空间,默认是?
{
public static void HelloTime(string s)
{
Console.WriteLine("Hello{0}!The time is {1} now",s,DateTime.Now);
}
public static void GoodbyeTime(string s)
{
Console.WriteLine("Goodbye{0}!The time is {1} now",s,DateTime.Now);
}
publicvoid SayTime(string s)
{
Console.WriteLine("{0}!The time is {1} now",s,DateTime.Now);
}
}
class TestDelegate
{
public static void Main()
{
TimeDelegate a = new TimeDelegate(Mytime.HelloTime);
Console.WriteLine("Invoking delegate a:");
a("A");
TimeDelegate b = new TimeDelegate(Mytime.GoodbyeTime);
Console.WriteLine("Invoking delegate b:");
b("B");
TimeDelegate c = a+b;
Console.WriteLine("Invoking delegate c:");
c("C");
c-=a;
Console.WriteLine("Invoking delegate c:");
c("C");
Mytime t = new Mytime();
TimeDelegate d= new TimeDelegate(t.SayTime);
Console.WriteLine("Invoking delegate d:");
d("D");
}
}
|