什么是C#委托,动态代理,闭包和函数指针之间的区别?

时间:2022-06-24 11:37:17

What are useful definitions for the common methods of passing a method or function as data, such as:

将方法或函数作为数据传递的常用方法有哪些有用的定义,例如:

  • Delegates
  • Closures
  • Function pointers
  • Invocation by dynamic proxy and
  • 通过动态代理调用和

  • First class methods?
  • 一流的方法?

3 个解决方案

#1


Function pointers lets you pass functions around like variables. Function pointer is basically legacy method to pass function around in languages that don't support first-class methods, such as C/C++.

函数指针允许您像变量一样传递函数。函数指针基本上是传统函数,用于在不支持第一类方法的语言中传递函数,例如C / C ++。

First class methods Basically means you can pass functions around like variables. Methods (loosely) mean functions. So this basically means first class functions. In simplest terms, it means functions are treated as "first class citizens", like variables. In the old days (C/C++), because we can't directly pass a function around, and we had to resort to workarounds like function pointers, we said functions weren't first-class citizens.

第一类方法基本上意味着您可以像变量一样传递函数。方法(松散地)意味着功能。所以这基本上意味着一流的功能。简单来说,它意味着函数被视为“一等公民”,就像变量一样。在过去(C / C ++),因为我们不能直接传递一个函数,而我们不得不求助于函数指针等变通方法,我们说函数不是一等公民。

Delegates is C#'s answer to first-class methods. Delegates are somewhat more powerful because it involves closures, consider the following code snippet:

代表是C#对一流方法的回答。代理更强大,因为它涉及闭包,请考虑以下代码片段:

void foo( int a )
{
   void bar() { writefln( a ); }
   call( &bar );
}

void call( void delegate() dg  ) { dg(); }
int main( char[][] args ) {
   foo( 100 );
}

Notice that bar can reference the local variable a because delegates can use closures.

请注意,bar可以引用局部变量a,因为委托可以使用闭包。

Closures can be very confusing at first. But the lazy-man's definition can be really simple. It basically means a variable can be available in the human-expected way. Put in other words, a variable can be referenced in places where they look like they would be present, by reading the structure of the source code. For example, looking at the code fragment above. If we didn't have closure, bar would not be able to reference a because a was only local to foo, but not bar, which is another function.

封闭起初可能会非常混乱。但懒人的定义可以非常简单。它基本上意味着变量可以以人类预期的方式获得。换句话说,通过读取源代码的结构,可以在它们看起来像它们存在的地方引用变量。例如,查看上面的代码片段。如果我们没有闭包,bar就无法引用a,因为a只是foo的本地,而不是bar,这是另一个函数。

Dynamic Proxy is the odd one out. It doesn't belong to these items. Explaining it requires some very long text. It stems from the famous Proxy Pattern. The problem with Proxy Pattern was that the Proxy class needs to be implementing the same interface as the Subject. Dynamic Proxy basically means using reflective approach to discover the Subject's method so that the ProxyPattern can be freed from being tied to the Subject's interface.

动态代理是奇怪的。它不属于这些项目。解释它需要一些非常长的文本。它源于着名的代理模式。 Proxy Pattern的问题在于Proxy类需要实现与Subject相同的接口。动态代理基本上意味着使用反射方法来发现Subject的方法,以便可以释放ProxyPattern与Subject的接口绑定。

#2


just the ones i know about:

只是我所知道的:

  • Function pointers: just that, a pointer to a piece of code. you jump to it, it executes. typed languages can enforce some parameter passing convention (i.e. C declarations)
  • 函数指针:就是说,指向一段代码的指针。你跳到它,它执行。类型语言可以强制执行一些参数传递约定(即C声明)

  • Closures: a function with some state paired. most naturally written in lexically scoped languages (i.e. Scheme, JavaScript, Lua). several closures can share the same state (or part of it), making it an easy way to implement OOP.
  • 闭包:一个配对状态的函数。最自然地用词汇范围的语言编写(即Scheme,JavaScript,Lua)。几个闭包可以共享相同的状态(或其中的一部分),使其成为实现OOP的简单方法。

  • First class methods: a closure created from an object instance and a method. some languages with both closures and a native OOP (Python, JavaScript) can create closures automatically.
  • 第一类方法:从对象实例和方法创建的闭包。一些具有闭包和本机OOP(Python,JavaScript)的语言可以自动创建闭包。

#3


Closure is a programming language concept. Delegate is its realization in MS.NET.

闭包是一种编程语言概念。 Delegate是它在MS.NET中的实现。

A Delegate in MS.NET is a strongly typed pointer to an object's method (a delegate instance points to both - an object and its method). There is also a way to combine several void delegate instances into one.

MS.NET中的委托是指向对象方法的强类型指针(委托实例指向两者 - 对象及其方法)。还有一种方法可以将多个void委托实例合并为一个。

#1


Function pointers lets you pass functions around like variables. Function pointer is basically legacy method to pass function around in languages that don't support first-class methods, such as C/C++.

函数指针允许您像变量一样传递函数。函数指针基本上是传统函数,用于在不支持第一类方法的语言中传递函数,例如C / C ++。

First class methods Basically means you can pass functions around like variables. Methods (loosely) mean functions. So this basically means first class functions. In simplest terms, it means functions are treated as "first class citizens", like variables. In the old days (C/C++), because we can't directly pass a function around, and we had to resort to workarounds like function pointers, we said functions weren't first-class citizens.

第一类方法基本上意味着您可以像变量一样传递函数。方法(松散地)意味着功能。所以这基本上意味着一流的功能。简单来说,它意味着函数被视为“一等公民”,就像变量一样。在过去(C / C ++),因为我们不能直接传递一个函数,而我们不得不求助于函数指针等变通方法,我们说函数不是一等公民。

Delegates is C#'s answer to first-class methods. Delegates are somewhat more powerful because it involves closures, consider the following code snippet:

代表是C#对一流方法的回答。代理更强大,因为它涉及闭包,请考虑以下代码片段:

void foo( int a )
{
   void bar() { writefln( a ); }
   call( &bar );
}

void call( void delegate() dg  ) { dg(); }
int main( char[][] args ) {
   foo( 100 );
}

Notice that bar can reference the local variable a because delegates can use closures.

请注意,bar可以引用局部变量a,因为委托可以使用闭包。

Closures can be very confusing at first. But the lazy-man's definition can be really simple. It basically means a variable can be available in the human-expected way. Put in other words, a variable can be referenced in places where they look like they would be present, by reading the structure of the source code. For example, looking at the code fragment above. If we didn't have closure, bar would not be able to reference a because a was only local to foo, but not bar, which is another function.

封闭起初可能会非常混乱。但懒人的定义可以非常简单。它基本上意味着变量可以以人类预期的方式获得。换句话说,通过读取源代码的结构,可以在它们看起来像它们存在的地方引用变量。例如,查看上面的代码片段。如果我们没有闭包,bar就无法引用a,因为a只是foo的本地,而不是bar,这是另一个函数。

Dynamic Proxy is the odd one out. It doesn't belong to these items. Explaining it requires some very long text. It stems from the famous Proxy Pattern. The problem with Proxy Pattern was that the Proxy class needs to be implementing the same interface as the Subject. Dynamic Proxy basically means using reflective approach to discover the Subject's method so that the ProxyPattern can be freed from being tied to the Subject's interface.

动态代理是奇怪的。它不属于这些项目。解释它需要一些非常长的文本。它源于着名的代理模式。 Proxy Pattern的问题在于Proxy类需要实现与Subject相同的接口。动态代理基本上意味着使用反射方法来发现Subject的方法,以便可以释放ProxyPattern与Subject的接口绑定。

#2


just the ones i know about:

只是我所知道的:

  • Function pointers: just that, a pointer to a piece of code. you jump to it, it executes. typed languages can enforce some parameter passing convention (i.e. C declarations)
  • 函数指针:就是说,指向一段代码的指针。你跳到它,它执行。类型语言可以强制执行一些参数传递约定(即C声明)

  • Closures: a function with some state paired. most naturally written in lexically scoped languages (i.e. Scheme, JavaScript, Lua). several closures can share the same state (or part of it), making it an easy way to implement OOP.
  • 闭包:一个配对状态的函数。最自然地用词汇范围的语言编写(即Scheme,JavaScript,Lua)。几个闭包可以共享相同的状态(或其中的一部分),使其成为实现OOP的简单方法。

  • First class methods: a closure created from an object instance and a method. some languages with both closures and a native OOP (Python, JavaScript) can create closures automatically.
  • 第一类方法:从对象实例和方法创建的闭包。一些具有闭包和本机OOP(Python,JavaScript)的语言可以自动创建闭包。

#3


Closure is a programming language concept. Delegate is its realization in MS.NET.

闭包是一种编程语言概念。 Delegate是它在MS.NET中的实现。

A Delegate in MS.NET is a strongly typed pointer to an object's method (a delegate instance points to both - an object and its method). There is also a way to combine several void delegate instances into one.

MS.NET中的委托是指向对象方法的强类型指针(委托实例指向两者 - 对象及其方法)。还有一种方法可以将多个void委托实例合并为一个。