The parameter passing mechanism of the method (C #)

Source: Internet
Author: User

Parameter passing mechanism of six methods value parameter, reference parameter, output parameter

//transfer mechanism of parametersusingSystem;classmethod{//value parameter, passing the value itself, without altering the values of the external variable     Public Static voidValuemethod (inti) {i++; }    //reference parameters, pass the data address, directly manipulate the data, the original value to change//Note that the value of string is not changed after the assignment.     Public Static voidReferencemethod (ref inti) {i++; }    //output parameters, the delivery is also the address, but the difference is that the operation must assign the initial value to the variable     Public Static voidOutputmethod ( out inti) {i=0;//assigning an initial valuei++; }    Static voidMain () {//test three kinds of transmission value methods        intI=0;        Valuemethod (i); Console.WriteLine ("i="+i);//output is 0, original value unchanged        intj=0; Referencemethod (refj); Console.WriteLine ("j="+J);//The output is 1, the original value is +1;        intK; Outputmethod ( outk); Console.WriteLine ("k="+K);//The assigned initial value is 0 and the output is 1.     }}

Variable number of parameters

To declare a method as a method that can accept a variable number of parameters, use the params keyword

/** Created by SharpDevelop. * User:yangq * DATE:2016/8/27 * time:23:57 * ~ Change this template use Tools | Options | Coding | Edit Standard Headers. */usingSystem;classmethod{Static intAddi (params int[] values)//the number of parameters can vary, stored as an array{//the parameters are written as (1) or (1,2,3,4,5)//method is to add all the parameters, return and value        intsum=0; foreach(intIinchvalues) sum+=i; returnsum; }    Static voidMain () {Console.WriteLine (Addi (1,2,3,4,5));//the output is the result of the.    }}

The parameter passing mechanism of the method (C #)

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.