C # usage of the keyword params and the difference between C # ref and out

Source: Internet
Author: User

The C # Paramsparamsparams 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. Example: the literal meaning is difficult to understand, so it is useful to look at the example. [Csharp] // keywords_params.cs using System; class App {public static void UseParams (params object [] list) {for (int I = 0; I <list. length; I ++) {Console. writeLine (list [I]) ;}} static void Main () {// generally, an object array is constructed first, use this array as the method parameter object [] arr = new object [3] {100, 'A', "keywords"}; UseParams (arr ); // After the params modifier is used, we can directly use a set of objects as parameters. // Of course, this set of parameters must meet the parameter requirements of the called method. UseParams (100, 'A', "keywords "); Console. Read () ;}} the refref 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. Attributes are not variables, so they cannot be passed as ref parameters. 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. For example, from the compilation perspective, the two methods in the following code are exactly the same. If you try to do so, the Code cannot be compiled. If one method uses the ref or out parameter, and the other method does not use the two parameters, you can perform the overload. Example: it is useful to pass the value type by reference, but ref is also useful for passing the reference type. This allows the called method to modify the object referenced by the reference, because the reference itself is passed by reference. [Csharp] // keywords_ref.cs using System; class App {public static void UseRef (ref int I) {I ++ = 100; Console. writeLine ("I = {0}", I);} static void Main () {int I = 10; // view the Console before calling the method. writeLine ("Before the method calling: I = {0}", I); UseRef (ref I); // view the value Console after the method is called. writeLine ("After the method calling: I = {0}", I); Console. read () ;}} the difference between C # ref and out is in C #. You can pass parameters through values or references. You can pass a parameter through reference to allow function members to change the parameter value and keep the change. To pass parameters through references, you can use the ref or out keyword. Both the ref and out keywords provide similar functions, and they also act like pointer variables in C. The difference between them is: 1. When using ref parameters, the input parameters must be initialized first. For out, initialization must be completed in the method. 2. When using ref and out, the Ref or Out keyword must be added to both the method parameters and the execution method. To match. 3. out is suitable for use where multiple retrun return values are required, while ref is used when the caller's reference is modified by the method to be called. Note: in C #, there are four types of method parameters passed: by value, by reference, and output parameter (by output ), array parameters (by array ). There is no additional modifier for the value passing parameter. The address passing parameter requires the modifier ref, the output parameter requires the modifier out, And the array parameter requires the modifier params. If the value of the parameter is changed during a method call, the parameter passed in to the method does not change after the method call is completed, but retains the original value. On the contrary, if the value of the parameter is changed during the method call process, the parameters for passing in the method also change after the call is completed. In fact, we can clearly see the meaning of the two from the name-the pass-through parameter transfers a copy of the call parameter, while the transfer parameter passes the memory address of the call parameter, this parameter points to the same storage location inside and outside the method. The ref method parameter keyword in the method parameter allows the method reference to pass to the same variable of the method. When the control is passed back to the call method, any changes made to the parameters in the method will be reflected in this variable. To use the ref parameter, you must explicitly pass the parameter as the ref parameter to the method. The ref parameter value is passed to the ref parameter. Parameters passed to the ref parameter must be initialized first. Compared with the out parameter, the latter parameter does not need to be explicitly initialized before being passed to the out parameter. Attributes are not variables and cannot be passed as ref parameters. If the declarations of the two methods are only different in their use of ref, an overload will occur. However, it is not possible to define a unique reload for ref and out. The out method parameter keyword on the out method parameter enables the method reference to pass to the same variable of the method. When the control is passed back to the call method, any changes made to the parameters in the method will be reflected in this variable. When you want the method to return multiple values, it is very useful to declare the out method. You can still return a value using the out parameter. A method can have more than one out parameter. To use the out parameter, you must use the parameter as the out parameter to pass it to the method explicitly. The value of the out parameter is not passed to the out parameter. You do not need to initialize the variable passed as the out parameter. However, the out parameter must be assigned a value before the method is returned. Attribute is not a variable and cannot be passed as an out Parameter

Related Article

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.