Object-oriented problem, cannot set object to empty in method body?

Source: Internet
Author: User

I call the Clear method to set p to null, which is still available in the program below and can be printed.

Reply content:

I call the Clear method to set p to null, which is still available in the program below and can be printed.

This is the code of C #, you so directly to the P pass in, the equivalent of Person p1 = p JS is the same, in fact, your function of P is only a shallow copy ah, if you want to direct the reference into the words, then you need to add one in front ref

Clear(ref p);static void Clear(ref Person  p){    

Your clear method inside that P (followed by P0 to express a convenient distinction) is not an incoming variable p, it is pointing to the same piece of memory with P, that is, pointing to the same object, but you in the method of the P0 is assigned to empty, P0 no longer point to that object, P has no effect, The local variable p in the Main method still points to the original object.

  • 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.