这是它的编译方式:
class Program
{
delegate void Message();
private static void GoodMooning()
{
Console.WriteLine("Доброе утро");
}
private static void GoodEvening()
{
Console.WriteLine("Добрый вечер");
}
static void Main(string[] args)
{
Message message;
if(DateTime.Now.Hour < 12)
{
message = GoodMooning;
}
else
{
message = GoodEvening;
}
Console.WriteLine(DateTime.Now.Hour);
message();
Console.ReadKey();
}
}
但它不会像这样编译并写出消息变量未初始化:
class Program
{
delegate void Message();
private static void GoodMooning()
{
Console.WriteLine("Доброе утро");
}
private static void GoodEvening()
{
Console.WriteLine("Добрый вечер");
}
static void Main(string[] args)
{
Message message;
int a = 2;
if(a < 12)
{
message = GoodMooning;
}
message();
Console.ReadKey();
}
}