C # language function parameter transfer

Source: Internet
Author: User
The transfer of function parameters in C # language is similar to that in many later generations of C language. function parameters in C # Are very exquisite. First, the parameter must be written in the brackets behind the function name. here we need to call it a form parameter. The parameter must have a parameter name and a clear type declaration. The parameter name is only visible within the function body. Therefore, using the same variable name anywhere except the function body will not cause a conflict. Each time a function is called, a real parameter must be passed to the form parameter in the function definition. By default, the C # parameter is passed as a value. The advantages and disadvantages of this method are equally obvious. In addition, some misunderstandings may occur from time to time when transferring the reference type. What is even more confusing is that since CLR does not support pointer types, how can we implement the usage of pointer passing in C/C ++? Don't worry. This article will answer these questions one by one. First, we will discuss the default value transfer and the advantages and disadvantages of this method, and explain the misunderstandings that may occur when the reference type is passed by default. Then, we will discuss how to use the ref keyword to pass a value type to the parameter as a reference type. Finally, we try to make a function return multiple values. In C/C ++, we often use pointers to achieve this purpose, here we will use the out keyword to review this wonderful feeling. Each time a function is called, a real parameter must be passed for each form parameter of the function. By default, the value transfer mechanism is used. That is to say, the value of the real parameter will be copied to the form parameter, so that we can get a local variable within the function. The value of this variable is equal to the value of the passed real parameter, but they are stored in different buckets. Therefore, what we do to function parameters is actually an operation to put local variables in the function, which will never affect the variable passed as the actual parameter in vitro. Looking at the example below, I will not pay too much. Using system; namespace CS language function parameter transfer {// <summary> /// class1 abstract description. /// </Summary> class example {static void main (string [] ARGs) {int argument = 5; example exp = new example (); system. console. writeline (argument); exp. fun1 (argument); system. console. writeline (argument);} public example () {} public void fun1 (INT parameter) {// The parameter operation is actually a modification to the local variable // It does not affect the variable parameter + = 5 passed by the function in vitro as a real parameter; system. console. writeline (parameter) ;}} but the value transfer mechanism has a significant disadvantage. It is mainly manifested in the transfer of value types. Our modifications to the parameters will disappear at the end of the function body execution. If we want this change to affect variables other than the function body passed as real parameters, we must pass the value type as the reference type. We will discuss it later. Another disadvantage of the value transfer mechanism may be that it is an advantage, manifested in the transfer of reference types. According to the value transfer mechanism, a variable of the reference type is actually a shortest copy. Do not mistakenly think that the entire object has been deeply copied. Function parameters only obtain the value of handle of the real parameter. That is to say, the local parameter is actually a handle of the reference type. It has the same value as the handle of the variable passed as the real parameter, point to the same object (two handle points to the same location on the stack ). In this way, the changes made to the parameters in the function will directly affect the object on the stack. After the function is complete, the local parameters disappear, and modifications to the object on the stack become persistent changes and are retained. When the value type is passed as the reference type, we do not want to worry about the function's parameter modification to disappear as the function ends. As a reference type, it is not difficult to do this, as we mentioned above. However, if it is a value-type parameter, it seems a little troublesome. In the past, in C/C ++, a pointer can be passed to achieve this goal. However, the CLR has explicitly removed the pointer. As compensation, C # provides us with the ref keyword. The ref keyword notifies the compiler that the arguments are passed as reference types rather than value types. The following program helps us explain the problem. Using system; passing of namespace CS language function parameters {class example {static void main (string [] ARGs) {int argument = 5; example exp = new example (); // first display argument system. console. writeline (argument); exp. fun2 (ref argument); // you must use the ref keyword system when passing parameters. console. writeline (argument); system. console. readline ();} public void fun1 (INT parameter) {// The parameter operation is actually a modification to the local variable // It does not affect the variable parameter + = 5 passed by the function in vitro as a real parameter; system. c Onsole. writeline (parameter);} public void fun2 (ref int parameter) {parameter + = 5; system. console. the writeline (parameter) ;}} function fun2 requires an int type parameter with the keyword ref. The main () function defines an integer variable argument, which will be passed to function fun2 () as a real parameter (). Before calling this function, the variable argument is first displayed and its value is 5. Then call the function fun2 () and pass the argument to the parameter. In this case, the function gets a local handle pointing to the argument integer variable. Inside the function, add parameter to 5 and then display it. The value is 10. The argument is displayed again after the function is returned, and its value is also 10. Let the function return multiple return values. Sometimes we may want a function to return multiple return values. In fact, this is impossible because a function can return only one return value. However, we do have a way to achieve this effect. The simplest method is as follows. Public int fun3 (ref int I, Int J) {I = J; return I + J;} We call this function in this way. Int I; int sum = exp. fun3 (Ref I, 10); system. console. writeline (I); system. console. writeline (SUM); after the function fun3 () is executed, we actually get the value of I and the value of I + J. In fact, this function is used to return two values. Another keyword is also very important. That is the out keyword. This keyword allows you to pass a reference type to a parameter that does not allocate space. This keyword can also be used to return multiple values. Public void fun4 (ref int I, out object OBJ) {I + = 5; OBJ = I. tostring (); system. console. writeline (I); system. console. writeline (OBJ);} the above method requires two parameters. The second parameter requires an object-type variable. There is an out keyword before this parameter. The compiler will think that the real parameters of this parameter are not allocated storage space. The out parameter cannot be used before being assigned a value. You can call this function as follows: int I = 5; object OBJ; exp. fun4 (Ref I, out OBJ); system. console. writeline (I); system. console. writeline (OBJ); the output is 4 to 10. After calling this function, we get the values of the variables I and obj.
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.