C# “调用”和“调用”有什么区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18505422/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What's the difference between "call" and "invoke"?
提问by John Smith
I'm currently reading a book by Daniel M. Solis called "Illustrated C# 2010." The book says:
我目前正在阅读 Daniel M. Solis 的一本书,名为“Illustrated C# 2010”。书中说:
"When a method is called or invoked ..."
“当一个方法被调用或调用时......”
What is the difference between these two terms?
这两个术语有什么区别?
回答by Deep Sharma
Function calling is when you call a function yourself in a program. While function invoking is when it gets called automatically.
函数调用是指您在程序中自己调用函数。而函数调用是它被自动调用的时候。
For example, consider this program:
例如,考虑这个程序:
struct s
{
int a,b,s;
s()
{
a=2;
b=3;
}
void sum()
{
s=a+b;
}
};
void main()
{
struct s obj; //line 1
obj.sum(); // line 2
}
Here, when line 1 is executed, the function (constructor, i.e. s) is invoked. When line 2 is executed, the function sum is called.
这里,当第1行执行时,函数(构造函数,即s)被调用。当第 2 行执行时,函数 sum 被调用。
source: web
来源:网络
回答by Alireza
Method Invokation is a term usually refered to indirectly calling a method(function) because of problems or difficulties in calling it directly.
方法调用是一个术语,通常指由于直接调用方法(函数)的问题或困难而间接调用方法(函数)。
For example in the context of Parallel programming:Consider two threads inside one application space are running parallely. Calling a public method of an object residing on aother thread throws Cross Thread Invokation Exception because race may occure. The solution is invoking the object to execute the method and yeild the rest of job to the object to manage parallel requests.
例如在并行编程的上下文中:考虑一个应用程序空间内的两个线程并行运行。调用驻留在另一个线程上的对象的公共方法会引发跨线程调用异常,因为可能会发生竞争。解决方案是调用对象来执行方法,并将剩余的工作交给对象来管理并行请求。
Another example is when you have a delegate pointing to a method somewhere. When you ask the delegate to call that (unknown) method, you Invoke the method to run.
另一个例子是当你有一个委托指向某个地方的方法时。当您要求委托调用该(未知)方法时,您调用该方法以运行。
回答by Jeppe Stig Nielsen
Maybe he simply considers the terms "call" and "invoke" synonymous, and just wants to mention both words because both terms can be encounter in the wild. Wouldn't it be possible to use orin that case?
也许他只是简单地认为“调用”和“调用”这两个词是同义词,而只想提及这两个词,因为这两个词都可以在野外遇到。在那种情况下不能使用或吗?
回答by Ben Seidel
From my research (personal & unpaid), looking at the common way these terms are used in programming literature & "in the wild", I have found that these definitions seem to fit their usages.
根据我的研究(个人和无偿),查看这些术语在编程文献和“野外”中的常用方式,我发现这些定义似乎适合它们的用法。
Executionrefers to the process of running code. Exact method does not matter, can be compiled or not, done by a computer or not.
执行是指运行代码的过程。具体方法无所谓,能不能编译,能不能用电脑完成。
Applying/Applicationrefers to the binding of arguments to the function. Application can be both partial and complete. From functional programming world, partial application produces another function with less parameters while complete application produces a thunk. Thunks are functions with no parameters and can help with "lazy evaluation".
Applying/Application是指将参数绑定到函数。申请可以是部分的,也可以是完整的。从函数式编程世界来看,部分应用程序产生另一个参数较少的函数,而完整应用程序产生一个 thunk。Thunk 是没有参数的函数,可以帮助进行“惰性求值”。
Invoking/Invocationrefers to the process required to schedule the function, with its fully bound arguments, for execution. Such systems include pushing arguments onto the stack and transferring the PC to the new address, placing messages/objects/functions/thunks on a queue for later execution or various other RPC systems. Exact mechanism does not matter. The notion of scheduling for future execution matters. Invoking requires that the will function execute.
调用/调用是指调度函数及其完全绑定的参数以供执行所需的过程。此类系统包括将参数推入堆栈并将 PC 传输到新地址,将消息/对象/函数/thunk 放在队列中供以后执行或其他各种 RPC 系统。确切的机制并不重要。未来执行的调度概念很重要。调用需要执行 will 函数。
Callingis the least defined out of the lot. Generally refers to the combined process of fully applying the function then invoking it, usually with the added semantic that your code will wait for a return value.
呼叫是最不明确的。通常是指完全应用函数然后调用它的组合过程,通常带有附加的语义,即您的代码将等待返回值。
Please also note that all of these terms are subjective from the point view of the current code that is being written. Invoking a function via a RPC call is only invoking it from the client's side. From the server's side the request has a different invocation point, if the function even has any "meaning" as a function on the server's side.
另请注意,从正在编写的当前代码的角度来看,所有这些术语都是主观的。通过 RPC 调用调用函数只是从客户端调用它。从服务器端来看,请求有一个不同的调用点,如果该函数在服务器端作为一个函数甚至具有任何“意义”。
回答by Behzad Sedighzadeh
When you execute the method in your code,directly, it's called Calling. When someone else executes it for you, it's Invoking. This is what I understand from Control.Invoke
method.
当您在代码中直接执行该方法时,它被称为Calling。当其他人为您执行它时,它是Invoking。这是我从Control.Invoke
方法中理解的。
回答by TimP
To "invoke" appears to mean to call a method indirectly through an intermediary mechanism. I'm sure the precise meaning gets blurred by authors. But, they must be trying to describe a different way of calling a method or the term wouldn't have arisen in the first place.
“调用”似乎意味着通过中间机制间接调用方法。我敢肯定作者会模糊准确的含义。但是,他们必须试图描述调用方法的不同方式,否则这个术语一开始就不会出现。
Also, the general (non computer) definition of "invoke" typically means to call out to a higher power for assistance. This would translate to asking an intermediary entity for help in getting something done.
此外,“调用”的一般(非计算机)定义通常意味着呼唤更高的力量寻求帮助。这将转化为向中间实体寻求帮助以完成某事。