C# 方法和功能的区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12258964/
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
Difference between Method and Function?
提问by Ammar Asjad
I am a beginner in c# and have a keen interest to learn c#, but I am confused. When I asked some one what the difference is between Function and method, he said to me that there is no difference, that they both have the same functionality.
Now I am quite confused and want to know from good developers what methods and functions are?
我是 c# 的初学者,对学习 c# 有浓厚的兴趣,但我很困惑。当我问某人 Function 和 method 之间有什么区别时,他告诉我没有区别,它们具有相同的功能。
现在我很困惑,想从优秀的开发人员那里知道什么是方法和函数?
Are they both the same? If not, then how do I initialize each one??
他们两个一样吗?如果没有,那么我如何初始化每个?
Is this way to initialize a function correct?
这种初始化函数的方法正确吗?
public void UpdateLeaveStatus(EmployeeLeave objUpdateLeaveStatus)
Please provide proper help as I am new.
由于我是新手,请提供适当的帮助。
采纳答案by Habib
Both are same, there is no difference its just a different term for the same thing in C#.
两者都是相同的,没有区别,只是在 C# 中对同一事物的不同术语。
方法:
In object-oriented programming, a methodis a subroutine (or procedure or function) associated with a class.
在面向对象的编程中,方法是与类关联的子例程(或过程或函数)。
With respect to Object Oriented programming the term "Method" is used, not functions.
对于面向对象编程,使用术语“方法”,而不是函数。
回答by Kirill Bestemyanov
There is no functions in c#. There is methods (typical method:public void UpdateLeaveStatus(EmployeeLeave objUpdateLeaveStatus)) link to msdnand functors - variable of type Func<>
c#中没有函数。有方法(典型方法:public void UpdateLeaveStatus(EmployeeLeave objUpdateLeaveStatus))链接到MSDN和函子-类型的可变Func<>
回答by Freeman
well, in some programming languages they are called functions others call it methods, the fact is they are the same thing. It just represents an abstractized form of reffering to a mathematical function:
好吧,在某些编程语言中,它们被称为函数,其他人则称之为方法,事实上它们是同一回事。它只是表示引用数学函数的抽象形式:
f -> f(N:N).
meaning its a function with values from natural numbers (just an example). So besides the name Its exactly the same thing, representing a block of code containing instructions in resolving your purpose.
这意味着它是一个具有自然数值的函数(只是一个例子)。因此,除了名称之外,它完全相同,表示包含解决您的目的的指令的代码块。
回答by Jamiec
Both are the same, both are a term which means to encapsulate some code into a unit of work which can be called from elsewhere.
两者都是一样的,都是一个术语,意思是将一些代码封装到一个可以从其他地方调用的工作单元中。
Historically, there may have been a subtle difference with a "method" being something which does not return a value, and a "function" one which does. in C# that would translate as:
从历史上看,“方法”是不返回值的东西,而“函数”是返回值的东西,这可能存在细微的差别。在 C# 中,这将转换为:
public void DoSomething() {} // method
public int DoSomethingAndReturnMeANumber(){} // function
But really, I re-iterate that there is really no difference in the 2 concepts.
但实际上,我再次重申,这两个概念确实没有区别。
回答by Vitali Kaspler
When a function is a part of a class, it's called a method.
当函数是类的一部分时,它被称为方法。
C# is an OOP language and doesn't have functions that are declared outside of classes, that's why all functions in C# are actually methods.
C# 是一种 OOP 语言,没有在类之外声明的函数,这就是为什么 C# 中的所有函数实际上都是方法。
Though, beside this formal difference, they are the same...
虽然,除了这种形式上的区别之外,它们是相同的......
回答by Sathyaseelan Kumaran
Programmers from structural programming language background know it as a function while in OOPS it's called a method.
具有结构化编程语言背景的程序员将其称为函数,而在 OOPS 中则称为方法。
But there's not any difference between the two.
但两者之间没有任何区别。
In the old days, methods did not return values and functions did. Now they both are used interchangeably.
在过去,方法没有返回值而函数有返回值。现在它们都可以互换使用。
回答by ha9u63ar
From Object-Oriented Programming Concept:
从面向对象的编程概念:
If you have a function that is accessing/muttating the fields of your class, it becomes method. Otherwise, it is a function.
如果您有一个访问/改变类字段的函数,它就变成了方法。否则,它是一个函数。
It will not be a crime if you keep calling all the functions in Java/C++ classes as methods. The reason is that you are directly/indirectly accessing/mutating class properties. So why not all the functions in Java/C++ classes are methods?
如果您继续将 Java/C++ 类中的所有函数作为方法调用,这不会是犯罪。原因是您直接/间接访问/改变类属性。那么为什么不是 Java/C++ 类中的所有函数都是方法呢?
回答by jdawg
In C#, they are interchangeable (although method is the proper term) because you cannot write a method without incorporating it into a class. If it were independent of a class, then it would be a function. Methods are functions that operate through a designated class.
在 C# 中,它们是可以互换的(尽管方法是正确的术语),因为如果不将方法合并到类中,就无法编写方法。如果它独立于一个类,那么它就是一个函数。方法是通过指定的类操作的函数。

