The difference between value passing and reference passing in C #

Source: Internet
Author: User

Passing parameters by value

When an argument is passed as a value, a new copy is created.

Class Test {

static void Main (string[] args) {

int x=8;

Fo (x);

Console.WriteLine ("X={0}", x);

}

static void Fo (int p)

{

p = p + 1;

Console.WriteLine ("P={0}", p);

}

The result of the program operation is: P=9,x=8, that is, the value of x is not affected by P, assigning a new value to p does not change the contents of X because P and x exist in different places in memory.

Similarly, when passing a reference type object in a pass-through method, it simply replicates the object itself, that is, its address value, not the object it refers to. The StringBuilder object that is seen in fo in the following code is the one instantiated in the main method, just a different reference to it. static void Fo (StringBuilder fosb)

{

Fosb.append ("test");

FOSB = null;

}

static void Main ()

{

StringBuilder sb = new StringBuilder ();

Fo (SB);

Console.WriteLine (sb.)  ToString ());

Run Result: Test.

In other words, SB and FOSB are different reference variables that point to the same object. Because FOSB is a referenced copy, it is set to null and the SB is not NULL.

Ref modifier

When you use the REL keyword, it means that the parameter is passed in a reference way. Arguments and formal parameters refer to the same object, changing one of the reference values and changing the other.  

static void Main (string[] args)

{

int x = 8;

Fo (ref x);

Console.WriteLine ("X={0}", x);

}

static void Fo (ref int p)

{

p = p + 1;

Console.WriteLine ("P={0}", p);

Run Result: p=9;x=9.

If you change the value of P in the function fo, the value of X will change as well. static void Main (string[] args)

{

int x = 8;

Fo (ref x);

Console.WriteLine ("X={0}", x);

}

static void Fo (ref int p)

{

p = 10;

p = p + 1;

Console.WriteLine ("P={0}", p);

Run Result: p=11,x=11;

The ref modifier must appear when writing functions and calling functions.

The ref modifier should primarily be in the method of implementing the interchange. static void Swap (ref string A, ref string B)

{

string temp = A;

A = b;

b = temp;

}

static void Main ()

{

string x = "Hello";

String y = "world";

Swap (ref x, ref y);

Console. WriteLine (x);

Console.WriteLine (y);

Run Result: World Hello

Out modifier

The out modifier is very similar to the ref modifier except for the following two points:

One, you do not need to assign a value when calling a function.

Second, you must assign a value before the function exits.

The out modifier is typically used when you need to get multiple return values from a method, static void Split (string name, out string firstName, out string lastName)

{

int I=name. LastIndexOf (");

FirstName =name. Substring (0,i);

LastName =name. Substring (i+1);

}

static void Main ()

{

String name = "Steven Francis";

String A, B;

Split (name, out a, out B);

Console. WriteLine (a);

Console. WriteLine (b);

Run Result: A=steven,b=francis Summary: 1, when a value is passed, why does the change in the parameter value in the called method not affect the corresponding argument?

Answer: because when passed by value, the system first allocates memory space for the parameters of the called method, and then copies the values in the arguments to the parameter by position one by one. The value stored in the parameter is just a copy of the argument, so any change to the parameter value in the called method will not affect the corresponding parameter.

  2, what is the difference between a value pass and a reference pass, what is a value parameter, and how is it passed? A: When a value is passed, the system first allocates a memory space for the parameter of the called method and copies the value of the argument to the parameter at position one by one, after which the parameters in the called method are worth nothing to affect the corresponding arguments, and when the reference is passed, the Instead of copying the value of the argument itself to the parameter, the system passes its reference value (that is, the address value) to the parameter, so that the variable referenced by the parameter is the same as the passed argument, and the corresponding parameter in the method body is worth any change that affects the argument passed as a reference.

  3, what is a formal parameter and what is an argument? a : parameters that are specified in the definition function are formal arguments, and they do not account for memory cells when function calls are not present, but only when a function call has occurred that the formal parameters in the function are assigned an inner deposit element. At the end of the call, the memory unit that the formal parameter occupies is also freed.
actual parameters: An argument can be a constant, a variable, and an expression, but requires a definite value. Assigns the value of the argument to the parameter at the time of invocation. In memory, the argument unit and the formal parameter unit are different units. When the function is called, the parameter is assigned a storage unit and the corresponding value of the argument is passed to the parameter, and after the call is finished, the parameter element is released and the argument unit retains its original value.
Understanding: The argument is the thing that is sent in the method ~ ~ The line parameter is to send in the thing in the method copy processing, after processing the method returns a thing-return value.

When the value is passed, the argument is constant ~ The parameter is changed with the calculation ~ ~ ~ When the reference is passed ~ ~ How the arguments change ~
The transfer of parameters is divided into: 1. Value mode parameter pass, 2. Reference method parameter pass.

The difference between value passing and reference passing in C #

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.