111,094
社区成员




void foo(int type)
{
dosth1();
switch (type)
{
case 0:
Console.WriteLine("0");
break;
case 1:
Console.WriteLine("1");
break;
}
dosth2();
}
foo(0);
foo(1);
void foo(MethodBase mb)
{
dosth1();
mb.Do();
dosth2();
}
class MethodBase
{
public virtual void Do() { }
}
class Method0 : MethodBase
{
public override void Do() { Console.WriteLine("0"); }
}
class Method1 : MethodBase
{
public override void Do() { Console.WriteLine("1"); }
}
foo(new Method0());
foo(new Method1());
void foo(int type)
{
dosth1();
switch (type)
{
case 0:
Console.WriteLine("0");
break;
case 1:
Console.WriteLine("1");
break;
}
dosth2();
}
foo(0);
foo(1);
void foo(Action action)
{
dosth1();
action();
dosth2();
}
foo(() => Console.WriteLine("0"));
foo(() => Console.WriteLine("1"));
private Dictionary<bool, Action> dic;
//确认时执行的代码
private void Confirm()
{
lit.Text = "确认";
}
//取消时执行的代码
private void Cancel()
{
lit.Text = "取消";
}
//赋值
dic = new Dictionary<bool, Action>();
dic.Add(true, Confirm);
dic.Add(false, Cancel);
//调用
dic[result]();