Visual C # 2005 Quick Start Call method

Source: Internet
Author: User
Tags argumentlist

The method exists just to be called! Use the method name to invoke a method that requires it to perform its task. If the method is to get information (specified by its arguments), it must provide the information it needs. If the method returns information (specified by its return type), it should somehow capture the information.

Specify method invocation Syntax

In order to invoke a C # method, you need to use the following syntax form:

methodName (argumentList)

The MethodName (method name) must be exactly the same as the name of the method that is invoked. Remember, the C # language is case-sensitive.

The argumentlist (parameter list) is used to provide optional information that will be received by the method. You must provide a parameter value (argument) for each parameter (formal parameter), and each parameter value must be compatible with the type of its corresponding formal parameter. If the method has two or more parameters, you must use commas to delimit different parameters when supplying the parameter values.

Important a pair of parentheses must be included in each method call, even if a method without parameters is invoked.

The Addvalues method is listed below again:

int addValues(int leftHandSide, int rightHandSide)
{
// ...
}

The Addvalues method has two int parameters, so when calling the method, you must supply two comma-delimited int arguments:

addValues(39, 3); // 正确方式

You can also replace the direct amount 39 and 3 with the name of an int variable. The values of these variables are passed as parameter values to the method, for example:

int arg1 = 99;
int arg2 = 1;
addValues(arg1, arg2);

The following is a list of some incorrect addvalues invocation methods:

addValues; // 编译时错误,无圆括号
addValues(); // 编译时错误,无足够实参
addValues(39); // 编译时错误,无足够实参
addValues("39", "3"); // 编译时错误,类型错误

The Addvalues method returns an int value. This int value can be used anywhere where an int value is available. For example:

result = addValues(39, 3); // 作为赋值操作符的右操作数
showResult(addValues(39, 3)); // 作为另一个方法调用的实参

In the following exercise, we will continue to use the Mathsoperators application. This time, we'll look at some method calls.

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.