0

我有一个带有返回函数的属性的类。

public class Demo
{
    public Func<string,int,bool> Something { get; set; }
}

如果我喜欢这样

Demo demo = new Demo();

string target;

demo.Something = (a,b)=>
{
   //in here `a` contains a value.
   //and I want to do:
   target = a;

   return true;
};


 //later in the code target is null
 //target here is null instead of having the value of `a`

如何将 a 的值分配给 lambda 中的目标变量,以便稍后在代码中重用它?

4

1 回答 1

0
 public static void Main(string[] args)
 {
     Demo demo = new Demo();

     string target;

     demo.Something = (a, b) =>
     {
         target = a;
         return true;
     };

     //Call something with params
     demo.Something("foo", 1);
 }
于 2016-11-14T23:25:35.717 回答