C # When a reference type is a parameter to a method

Source: Internet
Author: User

In C #(java), parameter passing is the value of the passed parameter itself.

For value types, the value itself is passed.

for reference types, when a reference type variable is defined, a variable in the stack is stored as a pointer to the address of an object instance allocated in the heap, unless, of course, the object is not instantiated, given a null value.

When passing a reference type variable, the value is passed,

But its value is the memory address, the address that specifies the object in the heap.

So when we change the content of the object in the method, our perimeter the object of the reference type variable operation has also changed because they are pointing to the same one.

But if we are in the method of operation to object is newly instantiated, it will be reborn in the heap into an object, and the original is two different objects, at the end of the operation method, if the object is not specially processed, there is no variable to point to it, it will be destroyed.

Example:

New Thread (() =>{try{stringbuilder sb = Null;//addline (ref SB); AddLine (SB); Rtb_log. Invokeifrequired (() = Rtb_log. AppendText (sb.) ToString () + "\ r \ n")); catch (Exception ex) {Rtb_log. Invokeifrequired (() = Rtb_log. AppendText (ex. Message + "\ r \ n")); finally {if (conn! = NULL && conn. state = = ConnectionState.Open) Conn. Close (); }}). Start ();   void AddLine (StringBuilder sb) {if (SB = = null) SB = new StringBuilder (); sb. Append ("Hello world!");}

occurs when it is called the exception for the object null reference.

The reason is because the initialization of the object in the method, but the periphery of SB and the method of SB is two different variables, in the method assigned to the object instance, the periphery of the SB specified or Null.

If you want to remove this exception, there are several methods, the first one is to effectively initialize the object, do not set the null value, if this can be directly new StringBuilder (). Do not instantiate inside the method. The second is if the object cannot be initialized in the perimeter, like an interface object, which needs to be deferred to the method for initialization, the way in which the return value can be used, or the method of the ref parameter.

For example:

void AddLine (ref StringBuilder SBX) {if (SBX = = null) SBX = new StringBuilder (); sbx. Append ("Hello world!");} or StringBuilder addline2 (StringBuilder sbx) {if (SBX = = null) SBX = new StringBuilder (); sbx. Append ("Hello world!"); return SBX;}

 

C # When a reference type is a parameter to a method

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.