.net 匿名delegate 可以修改的局部变量
发表于 2006-11-17 03:08 AM 作者: cat
代码:
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication5
{
class Program
{
public delegate string F(int i);
static void Main(string[] args)
{
F f = null;
f = NewMethod(f);
f(20);
Console.ReadLine();
}
private static F NewMethod(F f)
{
int a = 20;
f = delegate(int i) { Console.WriteLine(a); a = 30; return i.ToString(); };
Console.WriteLine(a);
Exec(f);
Console.WriteLine(a);
a = 10;
return f;
}
static void Exec(F f)
{
Console.WriteLine(f(10));
}
}
} 结果可以试试看,原理可以用reflector看
评论总数 0
评论
发表评论 |
作者为 cat 的最新文章
- C# events VS delegates (2006-11-17)
- .net 匿名delegate 可以修改的局部变量 (2006-11-17)
- 做个简单的黑白棋(2) (2006-02-04)
- 做个简单的黑白棋(1) (2006-02-02)
- Stored Procedure (2006-01-28)




