Three important parameters in C # methods: Out, ref, params

Source: Internet
Author: User

Note: For beginners, self-study Yu Shunji podcasts.

1.out parameters.

  Concept: If you return multiple values of the same type in a method, consider returning an array. However, when returning multiple values of different types, it is obvious that the returned array does not solve the problem, and an out parameter is introduced. The out parameter focuses on the ability to return several different types of values in a method.

  code example:

User login:   

    Main: Test (Numbers, out-max1, out-min1, out-sum1, out-avg1, out-B, out-S, out D);

    method:public static void Test (int[] nums, out int max, out int min, out int sum, out int avg, out bool B, out string s, out double D)
{...}

2.ref parameters.

 Concept: The ability to take a variable into a method to change, after the change is completed and then the changed value out of the method, the ref parameter requires that the method must be assigned to it, and the method can not be assigned to a value.

  code example:

   Main: Test (ref n1, ref N2);

    method:public static void Test (ref int n1, ref int n2)
{...}

3.params variable parameters.

  Concept: The elements of the argument list that are consistent with the variable parameter array type are treated as elements of the array. The params variable parameter must be the last element in the formal parameter list.

code example:

    Main: Test ("Zhang San", 158412,100,100,100);

    method:public static void Test (string name, int ID, params int[] score)
{...}

Three important parameters in C # methods: Out, ref, params

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.