C # basic knowledge and insight-passing parameters through Functions

Source: Internet
Author: User

C # basic knowledge and insight-passing parameters through Functions
1. A function and a function call are used to pass parameters in the original function. Copy the code static void Main (string [] args) {int num = 10; Test (num); // assign a value to the local variable before use // Test (10 ); // directly assign a value to the local variable} static void Test (int I) // I is equivalent to a local variable {I ++ ;} the copy code Test function defines an int type variable I as a parameter, which is equivalent to declaring a local variable, and must be assigned a value before using the local variable, this is why a value is assigned when a function is used (pass a value in ). What if I don't want to assign a value to a parameter? Then assign a value to the parameter (local variable) when the function declares the parameter, so that the parameter can be left blank when the function is called. Copy the code static void Main (string [] args) {int x = Test (); // you do not need to pass the value int y = Test (20 ); // You can also set the value to Console. writeLine (x); Console. writeLine (y); Console. read ();} static int Test (int I = 10) // assign a value to a local variable. when passing a parameter, you can not assign a value to I {I ++; return I ;} copy code Note: parameters with initial values must be placed on the rightmost side of the parameter list. Multiple parameters with initial values may exist. Purpose: assign an initial value to a parameter. You can assign values or do not assign values when calling the parameter. The function is to specify the default value for the parameter ,. Static void Test (int a, int B = 20, int I = 10) {// to do} 2. We know that the array length is immutable. When we pass an array as a parameter, it is not easy to change the length of the array. The params parameter array can specify the parameter length at will, and you do not need to define an array for parameter passing when passing parameters. Easy to use. For example, copy the code static void Main (string [] args) {int [] arr = new int [] {1, 2, 4, 5, 6}; Test1 (arr); Console. writeLine (); Test2 (, 4); Console. writeLine (); Test2 (1, 5, 7); Console. read ();} static void Test1 (int [] arr) {for (int I = 0; I <arr. length; I ++) {Console. write (arr [I] + "") ;}} static void Test2 (params int [] arr) // params Array {for (int I = 0; I <arr. length; I ++) {Console. write (arr [I] + "") ;}} Copy the params parameter array of the Code. When used as a parameter, the number of parameters is variable. Of course, it can also be 0, but when the params parameter array is used, A function has only one params parameter and must be placed at the end of the parameter list. Summary: The array decorated with params parameters is equivalent to a variable-length parameter array. It is a keyword for modifying parameters. It is used when you do not know the number of parameters or the number of parameters may change. Iii. When passing values, the ref and out parameter modifier only copies one value and assigns it to another variable. The other variable does not change the original value. For example: int num = 200; int a = num; a-= 100; Console. writeLine (num); // output 200 Console. writeLine (a); // output 100. However, if num represents the money in my wallet, I give it to someone else (int a = num ), when someone else uses 100 (a-= 100), what is the amount of money (num) in my wallet? The execution in the above method will lead to errors, and the money in the wallet will not change, because when the value is passed, only a copy of The num value is copied to, a does not change the num value. What should I do ??? If the value I pass is the num reference (which can be understood as the address), then a will change when num is changed. However, if int type is passed as a value, the ref and out parameters are required to convert it into reference. The ref and out parameters are used to change the value to the reference parameter. Rewrite the example of the wallet above. Copy the code static void Main (string [] args) {int num = 200; int res = Test (ref num); Console. writeLine (num); // output 100 Console. writeLine (res); // output 100 Console. read ();} static int Test (ref int a) {a-= 100; return a;} copy the code ref and assign the initial value before use. It focuses on changes, modify the value. When using ref, you must add ref before parameters in function declaration and call. The function of the out parameter is similar to that of the ref parameter. Unlike the ref parameter, the out parameter does not have to assign an initial value before use, but must assign a value to the out parameter before return. Therefore, the out parameter focuses on the output. Example of out modifier parameter: copy the code static void Main (string [] args) {int num = 200; int B; int a = TestOut (200, out B); Console. writeLine (a); // output 100 Console. writeLine (B); // output 200 Console. read ();} static int TestOut (int a, out int B) {B = a; a-= 100; return a;} copy the code summary: both the ref and out parameter modifiers pass the parameter by reference type, but the ref focuses on changing the value, while the out focuses on the output. A function has only one output value. To return multiple values, use the out parameter modifier. Ref must be assigned a value before use, and out must be assigned a value before return. When using and calling a function, the ref and out parameters must be added before the parameters.

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.