Value Transfer and reference Transfer

Source: Internet
Author: User

The value is passed, but the copy is passed, and the reference is passed, it is passed an address ~
Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Text;
 
Namespace ConsoleApplication10
{
Class Program
{
Static void Main (string [] args)
{
Int v = 42;
Int r = 42;
DoWorkVal (v );
DoWorkRef (ref r );
Console. WriteLine ("value transfer, result: {0 }! ", V );
Console. WriteLine ("passed by reference, the result is {0 }! ", R );
}
 
Static void DoWorkVal (int param)
{
Param + = 10;
}
 
Static void DoWorkRef (ref int param)
{
Param + = 10;
}
}
}
For the value transfer, only a copy is passed, so the void function does not return a value, so the original value remains unchanged when the v is output. for reference transfer, the address is passed in the past. Therefore, after the function DoWorkRef is called, the r will also change, so it will be + 10, so the output result is 52 ~

Author: "McDelfino"

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.