The difference between method parameter modifiers ref and out

Source: Internet
Author: User
Tags modifiers
Out and ref

1 out and ref the address of the variable (reference address), formal parameter changes, the actual parameters also change. Because they all refer to an address;
2 parameters with ref and out adornments can only pass variables that cannot pass constants.
3 The difference between out and ref
The out-decorated parameters must be modified in the method, and ref can be modified or not modified;
Out in the incoming parameter, the parameter is a local variable, you can not assign a value, because out must be assigned to the value;
A ref-decorated parameter must have an initial value in the argument before it can be invoked. It is not necessarily assigned a value because ref is decorated.

Code on:

 Class Program {static void Main (string[] args) {int[] Zs = {3, 13, 6, 45, 6, 7, 99, 1
            79};
            The difference, using ref-decorated parameters, must be assigned the initial value before use; int min=0, Max;
            When calling a method, also add ref and Max Getminandmax (Zs,ref min,out max);

            Console.WriteLine ("The largest value in this array is {0}, the minimum value is {1}", max,min);
        Console.readkey ();
        #region out parameter to find the maximum and minimum static void Getminandmax (int[] nums in the array, ref int min, out int max)
                {///incoming array is empty, returns 0 if (Nums = null) {min = 0;
                max = 0;
            Return
            //Bubble Sort: Interview with,//actual development: Array.Sort (Nums); for (int i = 0; i < Nums. Length-1; i++) {for (int j = 0; J < Nums. Length-1-I;  J + +) {if (Nums[j] > nums[j + 1]) {int
       temp = Nums[j];                 NUMS[J] = nums[j + 1];
                    Nums[j + 1] = temp;
            ()}//Find two min = nums[0]; max = Nums[nums.
        LENGTH-1]; } #endregion}



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.