Object-oriented problem. Cannot I set the object to null in the method body?

Source: Internet
Author: User
I call the Clear method to set p to null. It can still be used in the following program and printed out ..

I call the Clear method to set p to null. It can still be used in the following program and printed out ..

Reply content:

I call the Clear method to set p to null. It can still be used in the following program and printed out ..

This is the C # code. If you directly transfer the code to p, it is equivalentPerson p1 = pThis is also true for js. In fact, the p in your function is just a small copy. If you want to directly import the reference, you need to addRefAh

Clear (ref p); static void Clear (ref Person p) {p = null ;}

The p in your Clear method (which is easily distinguished by p0 later) is not the input variable p. It points to the same memory as p, that is, to the same object, however, if you leave p0 blank in the method, p0 will no longer point to that object, which has no effect on p. The local variable p in the main method still points to the original object.

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.