using System;
namespace ConsoleApplication
{
class Program
{
public delegate void mydel(int z);
static void Main(string[] args)
{
Program p = new Program();
Random r = new Random();
int k = r.Next(99);
mydel f;
if (k > 50)
{
f = new mydel(p.high);
}
if (k < 50)
{
f = new mydel(p.low);
}
//f(k) is underlined and Use of unassigned local variable 'f'
f(k);
}
//methods
public void high(int m)
{
Console.WriteLine("{0}>50", m);
}
public void low(int n)
{
Console.WriteLine("{0}<50", n);
}
}
}
我希望 f(k) 能够工作,因为我给了委托参数。为什么我会收到此错误?(在 f(k) 部分使用未分配的局部变量“f”)