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.