Functions in C #

Source: Internet
Author: User

Definition of functions in C #

Modifier return type function name ( parameter list )


About return types

1: If the return type of the function is not void, a return statement must be returned in the body of the function, and the type of the return value is either the return type of the function or it can be implicitly converted to the return type of the function. Otherwise, you need to cast to the return type of the function.

2: If the return type of the function is not void, then in the execution of the function body, each outlet must have a return statement to be returned value.


About parameters

1: The parameter is in the form: type parameter name.

2: Similar to C + +,Java , the parameters for the base data type are passed with the value. However, similar to C + + , you can use special methods to "reference" the parameters of the base data type by using the keyword ref or out to decorate the parameter (in C + + using the " & " notation, while in Java There is no such mechanism.)

Such as:

Publicvoid changevalue (ref int m, out int n)

Note the difference:ref requires that the parameter must already be initialized outside the function, while out requires reinitialization before the parameter is first used in the body of the function.

In addition, when the function is called, the argument list is also accompanied by the ref or out keyword.

3: Variable parameter

mutable parameters in C # are arrays decorated with the params keyword, and the mutable parameter must be the last parameter in the argument list.

such as:intsumvals (params int[] vals)



Functions in C #

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.