8,497
社区成员
发帖
与我相关
我的任务
分享
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string s1 = "!";
MyMethod(s =>
{
Console.WriteLine(s + s1); //它可以认识外部的变量,反过来刚才说了,是不行的。其实这是一个编译器魔术。如果不懂 Lambda 的实质,反过来的时候不行了就会奇怪了。
return null;
});
}
static void MyMethod(Func<string, object> call)
{
call("Hello world");
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
MyMethod(s =>
{
string s1 = s + "!";
Console.WriteLine(s1);
return null;
});
Console.WriteLine(s1); // 出错,它看不到 Lambda 里面的 s1
}
static void MyMethod(Func<string, object> call)
{
call("Hello world");
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string s = "!";
FuncWithIn(ref s);
}
static void FuncWithIn(ref string s1)
{
MyMethod(s =>
{
Console.WriteLine(s + s1); //出错,没办法使用 ref
return null;
});
}
static void MyMethod(Func<string, object> call)
{
call("Hello world");
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
MyMethod(s =>
{
Console.WriteLine(s);
goto Label1;
return null;
});
Label1:
Console.WriteLine("Label1 here.");
}
static void MyMethod(Func<string, object> call)
{
call("Hello world");
}
}
}