功能与方法的区别 [英] Difference betwwen Function and Method

查看:161
本文介绍了功能与方法的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨帅哥



i有疑问

功能与方法有什么区别?

如何找到它?



谢谢

hi dudes

i have a doubt that is
"What is the difference between function and method ? "
"How to find it ? "

Thanks

推荐答案

到OP:接下来是什么这看起来似乎是学术性的,但关键是C#没有对方法函数进行正式区分,其他编程语言需要要表达的语法不同。



与其他程序员进行通信时,如果描述一个将某个Type的值作为函数返回的方法,并参考方法可能会有用返回Type'void作为一种方法。



当他在大多数编程语言中指出常用时,Mehdi Gholam是绝对正确的,方法和函数之间的区别在于函数总是返回一个值,而一个方法不返回一个值。



但是,在C#中,没有sp区分方法和功能的特殊语法!所有过程(方法或函数)都必须指定一个返回类型:指定一个返回类型'void是一种告诉编译器方法不返回值的方法。



C#编程语言规范[ ^ ]描述了一种方法:



8.7.3方法



方法是会员实现可以由对象或类执行的计算或操作。方法有一个(可能是空的)形式参数列表,一个返回值(除非方法的返回类型为void),并且是静态的或非静态的静态方法是通过类访问的。非静态方法,也称为实例方法,可以通过类的实例访问。泛型方法(第25.6节)有一个或多个类型参数的列表。



C#规范经常使用术语功能离子成员,作为所有的首要总结类别:静态方法,静态属性访问器和用户定义的运算符。实例功能成员。这些是实例方法,实例构造函数,实例属性访问器和索引器访问器。请参阅第14.4节。



在C#规范中没有任何地方可以找到关于函数的讨论 与方法相比。而且,你也不会在规范中找到匿名函数或lambda这个术语。



在C#方法中可以在Classes和Structs中定义;并且,是的,Structs可以有静态方法,即使Struct不能被定义为static:我从来没有找到使用该功能的理由:)
To the OP: what follows here may seem "academic," but the key point is that C# does not make the formal distinction between methods and functions that other programming languages require different syntax to express.

In communication with other programmers it may be useful if you describe a method that returns a value of some Type as a function, and refer to methods that return Type 'void as a method.

Mehdi Gholam is absolutely right when he points out that in common usage, in most programming languages, the distinction between methods and functions is that a function always returns a value, and a method does not return a value.

But, in C#, there's no special syntax for distinguishing between methods and functions ! All procedures (methods or functions) must specify a return Type: specifying a return Type of 'void is a way of telling the compiler that a method does not return a value.

The C# Programming Language specification [^] describes a method as:

"8.7.3 Methods

A method is a member that implements a computation or action that can be performed by an object or class. Methods have a (possibly empty) list of formal parameters, a return value (unless the method’s return-type is void), and are either static or non-static. Static methods are accessed through the class. Non-static methods, which are also called instance methods, are accessed through instances of the class. A generic method (§25.6) has a list of one or more type parameters."

The C# spec does frequently use the term "function members," as an over-arching summary category for all of: "static methods, static property accessors, and user-defined operators. Instance function members. These are instance methods, instance constructors, instance property accessors, and indexer accessors." See section 14.4.

Nowhere in the C# specification will you find a discussion of "functions" compared to "methods." And, you won't find the term "anonymous functions," or "lambda" in the spec, either.

In C# methods can be defined in both Classes, and Structs; and, yes, Structs can have static methods, even though a Struct cannot be defined as static: I've never been able to find a reason to use that feature :)


常用的定义,无论它们是否正确可以是:



方法是执行某些操作的类的成员。 method可以是一个子例程(或过程),一个不返回值的函数,或者一个函数可以被认为是一个返回值的过程。
The commonly used definitions, be they correct or not CAN be:

A method is a member of a class that performs some action. A method can either be a subroutine (or procedure), something which does not return a value, or a function can be thought of as a procedure which does return a value.


语义上 function 返回值和方法没有,虽然在.net CLR中没有区别,它们都被称为方法。
Semantically a function returns values and a method does not, although in the .net CLR there is no distinction and they are both called methods.


这篇关于功能与方法的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆