What is the difference between Method and function in .net
我在谷歌上搜索方法和功能的区别,得到了两个答案。
答案1方法是如下所示的非返回类型
1 2 3 | void Method() { } |
其中as函数是如下返回类型
1 2 3 | int Method() { } |
答案2
这两个术语没有区别。
查询-哪个是正确的,或者有第三件事?
没有区别。
在C语言和Java语言中,通常称之为方法。在JavaScript和C++中,它们通常被称为函数。
不管怎样,人们都会明白你的意思
< BR>
至于答案1,如果我是一个全新的程序员,并且我被要求推测不同之处,那么答案1可能就是我想到的答案。给出了函数作为一个过程的数学定义,在这个过程中,输入精确地决定了输出。
我认为你真正要问的是,函数和C中的动作(两者都是方法)之间的区别是什么。
区别与处理void的方式有关:这解释得很好https://softwarengineering.stackexchange.com/questions/131036/why-is-void-not-allowed-as-a-generic-type-in-c
我会把埃里克·利珀特的答案全部贴出来,以保存点击:
The fundamental problem with"void" is that it does not mean the same thing as any other return type."void" means"if this method
returns then it returns no value at all." Not null; null is a value.
It returns no value whatsoever.This really messes up the type system. A type system is essentially a
system for making logical deductions about what operations are valid
on particular values; a void returning method doesn't return a value,
so the question"what operations are valid on this thing?" don't make
any sense at all. There's no"thing" for there to be an operation on,
valid or invalid.Moreover, this messes up the runtime something fierce. The .NET
runtine is an implementation of the Virtual Execution System, which is
specified as a stack machine. That is, a virtual machine where the
operations are all characterized in terms of their effect on an
evaluation stack. (Of course in practice the machine will be
implemented on a machine with both stack and registers, but the
virtual execution system assumes just a stack.) The effect of a call
to a void method is fundamentally different than the effect of a call
to a non-void method; a non-void method always puts something on the
stack, which might need to be popped off. A void method never puts
something on the stack. And therefore the compiler cannot treat void
and non-void methods the same in the case where the method's returned
value is ignored; if the method is void then there is no return value
so there must be no pop.For all these reasons,"void" is not a type that can be instantiated;
it has no values, that's its whole point. It's not convertible to
object, and a void returning method can never, ever be treated
polymorphically with a non-void-returning method because doing so
corrupts the stack!Thus, void cannot be used as a type argument, which is a shame, as you
note. It would be very convenient.With the benefit of hindsight, it would have been better for all
concerned if instead of nothing whatsoever, a void-returning method
automatically returned"Unit", a magical singleton reference type. You
would then know that every method call puts something on the stack,
you would know that every method call returns something that could be
assigned to a variable of object type, and of course Unit could be
used as a type argument, so there would be no need to have separate
Action and Func delegate types. Sadly, that's not the world we're in.For some more thoughts in this vein see
http://blogs.msdn.com/b/ericlippert/archive/2009/06/29/the-void-is-invariant.aspx
and
http://blogs.msdn.com/b/ericlippert/archive/2011/02/21/never-say-never-part-one.aspx
and
http://blogs.msdn.com/b/ericlippert/archive/2011/11/28/why-have-a-stack.aspx
在.NET中,这些术语几乎是同义词,但在JavaScript等语言中,它们的含义可能略有不同。一个(JS术语)示例是,方法通常是包含函数的对象属性。
在这里,您还可以将表示为函数的差异表示为功能的独立单元,而将方法定义为类的成员。在。NET,您通常会听到"method"一词比"function"更常用(不过,它们确实有一个合适的名称"anonymous functions")。
这里还有另一个关于函数和方法差异的问题,您可能希望了解更多细节。
这真的取决于一个术语。第一个答案是正确的,但有些人也不认为它是100%正确的,使用
因此,在底层方法中,您可以执行以下操作:
1 2 3 4 | void DoSomething() { int i = Sum(1, 2); } |
然后有一个返回方法,可以确定为函数:
1 2 3 4 | int Method(int i, int j) { return i + j; } |
所以要回答你的问题,两者都不正确。这就是你如何解释它们,因为许多开发人员往往会改变他们所说的话。但是,vb.net使用
这两个术语没有区别。就其含义而言,它们是可以互换的。
我发现,在处理非托管代码(C/C++)时,人们会把它们称为函数。但是,当处理托管代码(Cy/Java/Java)时,人们会把它们称为方法。
我在Pascal中遇到了类似于
在C中,通常都称为方法。术语"函数"通常是为委托保留的,通常分为函数和操作-在这里您可以再次找到与您的
我特意用了这个词很多次,因为人们经常从其他语言中带来术语,但我写的是我在印刷C文献(如C 4.0)中最常遇到的东西。
一直以为是答案1,我无耻地,偶尔用它们互换。
这两个术语没有区别。