Param ref out keyword

Source: Internet
Author: User

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.

 

 

 

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.