Reference for transferring values in C #

Source: Internet
Author: User

In C #, the default value is pass by value. What is pass by value? We all know that during parameter passing, the form parameter and the real parameter occupy the storage space in different locations. During the calling process, the value of the form parameter is changed during execution, but it does not affect the real parameter values of the calling process (main call process. Therefore, when the call process ends and returns to the call process, the real parameter is still the value before the call. Therefore, in order to protect real parameters from being affected by the call process, the parameter passing method should be selected. The theory is somewhat abstract. Let me use a simple Swap function to explain it to you.

Class Program {static void Main (string [] args) {double a, B; Console. writeLine ("Enter the first digit a"); a = Convert. toDouble (Console. readLine (); Console. writeLine ("enter the second digit B"); B = Convert. toDouble (Console. readLine (); Swap (a, B); Console. writeLine ("the value of a after the main call process is passed by value" + a); Console. writeLine ("the value of B after the main call is passed by value" + B); Console. read ();} static void Swap (double x, double y) {double temp; temp = x; x = y; y = temp; Console. writeLine ("value of a in the call process" + x); Console. writeLine ("value of B in the call process" + y );}}

 

Output results: The results show that the values of a and B are only changed during the call process and remain unchanged after they are returned to the master call process. Next we will talk about transferring data by reference. In C #, the keyword ref must be added for passing by reference. The so-called pass by reference means that when a process is called, the memory address of the real variable is passed to the shape parameter of the called process, that is, the form parameter and the real parameter use the memory unit with the same address. Therefore, changing the value of the parameter during the call process is equivalent to changing the value of the real parameter. Let's use examples to illustrate the problem. In the above example, I will slightly modify it.
Class Program {static void Main (string [] args) {double a, B; Console. writeLine ("Enter the first digit a"); a = Convert. toDouble (Console. readLine (); Console. writeLine ("enter the second digit B"); B = Convert. toDouble (Console. readLine (); Swap (ref a, ref B); Console. writeLine ("the value of a after the main call is passed by reference" + a); Console. writeLine ("the value of B after the main call is passed by reference" + B); Console. read ();} static void Swap (ref double x, ref double y) {double temp; temp = x; x = y; y = temp; Console. writeLine ("value of a in the call process" + x); Console. writeLine ("value of B in the call process" + y );}}

 

Output result: if there is only one more ref, the result is the opposite. This is the magic of the Code. In summary, the parameter is passed by value, and the parameter is passed by reference. Hope to help you.

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.