Dark Horse programmer _ talking about out parameters, ref parameters and variable parameters

Source: Internet
Author: User

1. out parameters

Out keywordThis will cause the parameter to be passed through reference. In general, it is to pass the value out.

Function of the out Parameter: Used to assign values to external variables for internal variables. Out is generally applicable to situations where a method does not only return a single value but multiple return values.

Out ParameterPay attention to the following points:

1) outParameters do not need to be initialized before being passed.

Analysis:Before calling a methodOutVariables passed by parameters only need to be declared, and values can be assigned or not assigned. However, they must be overwritten during the call. Therefore, you do not need to assign values because no error is reported even if values are assigned, however, it is useless at all, and there is no need to do so! (See the following code)

2)If the method parameters are usedOutWhen modifying the keyword, rememberOutAdd out before the variable passed by the parameter.

Analysis:When an out parameter is added, it indicates that the parameter is not passed in, but the output value, but the out parameter can still be passed in. If the out keyword is not added, this parameter becomes invalid and an error is returned.

3)Method usedOutWhen a parameter is used to pass a variable, it is to return the value. Therefore, the variable modified by the out must be assigned a value before the return value of the method or the end of the method.

1 static void main (string [] ARGs) 2 {3 int RST; // The variable modified by out needs to be declared before the call. 4 int num = 8; 5 If (ismod (Num, out RST) // remember to add the out modifier variable 6 {7 console. writeline ("{0} is an even number, {0}/2 = {1}", num, RST); 8} 9 else10 {11 console. writeline ("{0} is not an even number", num); 12} 13 console. readkey (); 14} 1516 public static bool ismod (INT number, out int result) // return 17 {18 if (Number % 2 = 0) through the Parameter) 19 {20 result = Number/2; // You must assign 21 return true to result before the method ends; 22} 23 else24 {25 result = 0; // The result must be assigned 26 return false; 27} 28} before the method ends}

In the above Code, The out variable modifies the two variables rst and result, which is equivalent to establishing a connection between them, assigning values to the result, that is, assigning values to the rst, it is equal to returning the value of the local variable in the method, which successfully achieves the goal of returning the value through the parameter.

4) In addition, if you want to use the out variable in the method, such as the above program, you must assign a value to the variable before using the result in the ismod method. Although the RST has been assigned a value before the ismod method is called, for example, RST = 2. When the RST parameter is passed in as an out, the result can also receive a value from rst, that is, the call result is equal to 2. However, to use the result variable in the ismod method, you must first assign a value to it, this makes it meaningless to assign values before calling.

2. Ref Parameters

RefKeywordsSame as out, the parameter can be passed through reference. In other words, ref allows you to directly operate on the original number, instead of copying the original number to operate on that copy.

RefFunction of parameters: Different from out, it can transmit values in two directions, which is mainly applicable to internal changes to external values in the scenario.

RefParametersPay attention to the following points:

1)RefBefore passing a parameter, the variable must be initialized and assigned a value.

Analysis:Ref is applicable to changes to external values in the scenario. Therefore, before passing a parameter, you must assign an initial value to it before passing the value.

2) If the method parameters are usedRefWhen modifying the keyword, rememberRefBefore the variable passed by the parameter, addRefOnly.

Analysis:The addition of ref indicates that this parameter is both input and output. Without the ref keyword, this parameter becomes invalid and an error is returned, both the num and number variables are modified using ref, so that the number points to the num. Therefore, when performing operations on the number in the test method, the num operation is performed. (See the following code)

1 static void main (string [] ARGs) 2 {3 int num = 100; // for a variable modified by REF, the initial value 4 test (ref num) must be assigned to it ); // remember to add the ref variable 5 console. writeline (Num); 6 console. readkey (); 7} 8 9 public static void test (ref int number) 10 {11 int NUMA = number; 12 number = 2; // not necessarily assigned 13}

3) The method is used.RefWhen a parameter is passed, it is different fromOut, It is not required thatRefThe variable to be modified is assigned a value, so no value can be assigned.

4) In addition, if you want to useRefVariable modified, such as the above program, before using number in the test method, because the initial value of 100 is required for the incoming num, different fromOutIn this case, you can directly use the number variable in the test method.RefWhen passed in, number will receive the value from num, that is, number is equal to 100 during the call, then the value of number will be changed, and the value of num will also be changed.

3. variable parameters

ParamsIs a keyword that allows you to use an array parameter when the number of parameters is variable, such as int sum (Params int [] values ).

Variable parameters:So that variable parameters with uncertain numbers are transmitted as arrays. When the number of parameters in a method is unknown, it is used to transmit a large number of array set parameters.

Note the following when using variable parameters:

1) do not allowParamsModifier andRefAndOutModifier combination.

2) when there are multiple parameters in the parameter list of the method,ParamsThe modified parameters must be placed at the end and not at the front, so that the program cannot determine the number of parameters and report an error. For example: int sum (string name, Params int [] values)

3) Of course, a method can contain variable parameters and constant parameters, which can be used at the same time.

In this way, with variable parameters, you can determine the number of parameters of a method based on specific needs in some cases.

Summary:

This is my first learning diary. The knowledge points may not be very detailed, but each of them is summed up by myself. I have learned only a little c ++ basics before, I have never touched C #, and I do not know the three concepts of out parameter, ref parameter, and variable parameter. By learning the C # programming Basics of the dark horse basic getting started video, and reading relevant materials, I have been able to master these three knowledge points. I still remember that I was jumping to watch the video and could not understand the call of Int. why do I add out and out before the parameter in the tryparse method? Now I can use them clearly. Even though this is simple, it is a very important foundation. I need to record it to remind myself. It is found that writing study notes can not only improve your expression ability, but also help you learn.

I told myself to stick to it!

For details, see www.itheima.com.

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.