代码之家  ›  专栏  ›  技术社区  ›  anthony

“关闭”这个词的对立面是什么?

  •  1
  • anthony  · 技术社区  · 15 年前

    考虑以下(C#)代码。传递给concurvatedrand()的lambda被称为“closed over”名为format的变量。你会用什么术语来描述变量random是如何在MyMethod()中使用的?

    void MyMethod
    {
        int random;
        string format = "The number {0} inside the lambda scope";
    
        ConvolutedRand(x =>
        {
            Console.WriteLine(format, x);
            random = x;
        });
    
        Console.WriteLine("The number is {0} outside the lambda scope", random);
    }
    
    void ConvolutedRand(Action<int> action)
    {
        int random = new Random.Next();
        action(random);
    }
    
    2 回复  |  直到 15 年前
        1
  •  3
  •   Brian    15 年前

    我通常会在特定的表达或词汇范围的上下文中听到“束缚”和“自由”。lambda关闭了两个 format random (这是'自由'在lambda,这就是为什么它关闭了他们)。在MyMethod中,两个变量都只是局部绑定的变量。

        2
  •  1
  •   leppie    15 年前

    not free 也许吧?)