C # what happened when the parameter is passed

Source: Internet
Author: User

 

1. variables of the reference type only contain the memory address of the object. The memory address instead of the object itself will be copied, so modifications to the underlying object will be retained.

Unsafe class Program

{

Static void Main (string [] args)

{

Employee myE = new Employee () {id = 4 };

 

Fixed (int * pId = & (myE. id ))

{

Console. writeLine ("before method execution, the id address in myE is 0x {0: X} and the value is {1}", (uint) & (* pId), (uint) * pId );

}

TestMethod (myE );

Fixed (int * pId = & (myE. id ))

{

Console. writeLine ("after the method is executed, the id address in myE is 0x {0: X} and the value is {1}", (uint) & (* pId), (uint) * pId );

}

Console. ReadLine ();

}

Static void testMethod (Employee myE)

{

MyE. id = 5;

// MyE = new Employee () {id = 5 };

Fixed (int * pId = & (myE. id ))

{

Console. writeLine ("in method execution, the id address in myE is 0x {0: X}, and the value is {1}", (uint) & (* pId), (uint) * pId );

}

}

}

Output:

Before the method is executed, the id address in myE is 0x1AF5BE0 and the value is 4.

In method execution, the id address in myE is 0x1AF5BE0 and the value is 5.

After the method is executed, the id address in myE is 0x1AF5BE0 and the value is 5.

The execution process is as follows:

1) create an object myE In the heap and create a pointer to myE in the stack.

2) pass by reference, so create a pointer in the stack, copy the address of the pointer above, also point to myE

As shown in:

 

2. If the memory address inside the method points to a new object, the changes made in the method after the method ends will be discarded (the original pointer still points to the original object, method parameters point to the new object and are destroyed together ).

Unsafe class Program

{

Static void Main (string [] args)

{

Employee myE = new Employee () {id = 4 };

 

Fixed (int * pId = & (myE. id ))

{

Console. writeLine ("before method execution, the id address in myE is 0x {0: X} and the value is {1}", (uint) & (* pId), (uint) * pId );

}

TestMethod (myE );

Fixed (int * pId = & (myE. id ))

{

Console. writeLine ("after the method is executed, the id address in myE is 0x {0: X} and the value is {1}", (uint) & (* pId), (uint) * pId );

}

Console. ReadLine ();

}

Static void testMethod (Employee myE)

{

// MyE. id = 5;

MyE = new Employee () {id = 5 };

Fixed (int * pId = & (myE. id ))

{

Console. writeLine ("in method execution, the id address in myE is 0x {0: X}, and the value is {1}", (uint) & (* pId), (uint) * pId );

}

}

}

Output:

Before the method is executed, the id address in myE is 0x1E77BE0 and the value is 4.

In method execution, the id address in myE is 0x1E78E24 and the value is 5.

After the method is executed, the id address in myE is 0x1E77BE0 and the value is 4.

The execution process is as follows:

1) create an object myE In the heap and create a pointer to myE in the stack.

2) pass by reference, so create a pointer in the stack, copy the address of the pointer above, also point to myE

3) when a new object is added to the method, create an object in the heap and pass the address to pointer 2.

 



From heaven on the wall

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.