Thoughts on the C # Ref Parameter

Source: Internet
Author: User
First look at the Code:

Using System;
Using System. Collections. Generic;
Using System. Text;

Namespace testRef {

Class testRef {

Private static void ChgArrVal (int [] array ){
For (int I = 0; I <array. Length; I ++)
Array [I] = (int) Math. Pow (array [I], 2 );
}

Private static void ChgVal (int I ){
I = (int) Math. Pow (2, I );
}

Private static void ChgValRef (ref int I ){
I = (int) Math. Pow (2, I );
}

Static void Main (string [] args ){

Int [] arr = new int [] {3, 4, 5, 6, 7, 8 };
Int val = 5;

Console. WriteLine ("original:" + val );
ChgVal (val );
Console. WriteLine ("ChgVal:" + val );

Console. WriteLine ("#####");

Console. WriteLine ("original:" + val );
ChgValRef (ref val );
Console. WriteLine ("ChgValRef:" + val );

Console. WriteLine ("#####");

Foreach (int a in arr)
Console. WriteLine (">>>" + );

ChgArrVal (arr );
Console. WriteLine ("#####");

Foreach (int B in arr)
Console. WriteLine (">>>" + B );
}
}
}

------------------- The following is the running result:
Output:

Original: 5
ChgVal: 5
#####
Original: 5
ChgValRef: 32
#####
>>> 3
>>> 4
>>> 5
>>> 6
>>> 7
>>> 8
#####
>>>> 9
>>>> 16
>>>> 25
>>>> 36
>>>> 49
>>>> 64
Why...
###########################
Did you see it? The original Ref intent is
The ref keyword causes arguments to be passed by reference. the parameter reference is passed, that is, the reference is passed and executed. The original variable value can be changed without the return value. but you can view the array without using Ref. it can be seen that when an array is used, it is just an address reference, and it is not an actual operation. Why not use Ref to change it?
I don't know, right? I think so.

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.