During code writing, you may need to change the number of parameters (the same type of parameters). You can use the param keyword to solve this problem.
For example, the following code is often used in the development of AE:
Object missing = system. type. missing;
Ipointcollection Ppoints = new polylineclass ();
Ppoints. addpoint (ppoint1, ref missing, ref missing );
Ppoints. addpoint (ppoint2, ref missing, ref missing );
Ppoints. addpoint (ppoint3, ref missing, ref missing );
Ppoints. addpoint (ppoint4, ref missing, ref missing );
You can use the param keyword to write the following methods:
Public static getpointcollection (Params ipoint [] points)
{
Ipointcollection Ppoints = polylineclass ();
Object missing = system. type. missing;
Foreach (ipoint point in points)
{
Ppoints. addpoint (point, ref missing, ref missing );
}
Return Ppoints;
}
Then the main code is called in the following way;
Ipointcollection Ppoints = getpointcollection (ppoint1, ppoint2, ppoint3, ppoint4 );
------------------ Ref out Param ----------------- excerpt -----------------------
Ref is the address for passing parameters, and out is the return value. There are some similarities between the two, but there are also differences. You must assign values to variables before using ref.
The ref keyword allows the parameter to be passed by reference. The effect is that when the control is passed back to the call method, any changes made to the parameters in the method will be reflected in the variable. To use the ref parameter, you must explicitly use the ref keyword for both method definition and call methods.
Parameters passed to the ref parameter must be initialized first. Unlike out, out parameters do not need to be explicitly initialized before being passed.
While ref and out are processed differently at runtime, they are processed in the same way during compilation. Therefore, if one method uses the ref parameter while the other method uses the out parameter, the two methods cannot be reloaded.
The out function will clear the variable, even if the variable has been assigned a value, it will not work. When exiting the function, all variables referenced by the out function will be assigned a value, and the ref reference can be modified or not modified.
The out keyword causes the parameter to be passed through reference. This is similar to the ref keyword, except that the ref requires that the variables must be initialized before being passed. To use the out parameter, the out keyword must be explicitly used for method definition and call.
Although the variables passed as the out parameter do not need to be initialized before being passed, you need to call a method to assign values before the method returns.
The Params keyword specifies that the parameter method parameter is used when the number of parameters is variable.
After the Params keyword in the method declaration, no other parameters are allowed, and only one Params keyword is allowed in the method declaration.