更新时间:2018-04-18 09:03:19浏览次数:1+次
using System;
namespace ConsoleApp1
{
public class MyTest2
{
public MyTest2(Func<string> func)
{
if(func==null)
{
throw new ArgumentNullException("error ... ");
}
func();
Console.WriteLine("successful ... ");
}
}
public class MyTest
{
public Func<string> _func = Func_Test;
private static MyTest _instance = new MyTest();
public MyTest() : this(null)
{
Console.WriteLine("Construction .... ");
}
public MyTest(MyTest2 s)
{
MyTest2 _s = s ?? new MyTest2(_func);
}
public static MyTest Current
{
get { return _instance; }
}
public static string text
{
get { return "text .... "; }
}
public void Beta()
{
Console.WriteLine("function be called");
}
public static string Func_Test()
{
return null;
}
}
class Program
{
static void Main(string[] args)
{
MyTest.Current.Beta();
Console.Read();
}
}
}
我一开始就看不懂在_func属性明明就是返回来了null,在MyTest2中的if判断应该成立啊,后来断点调试看了下:public Func<string> _func = () => null;
改成了:public Func<string> _func = Func_Test;
后来才反应过来,原来传递的参数根本就是一个函数地址而已,if中的null是用来判断是不是没有传参的。相关资讯