Byval or byref?

Source: Internet
Author: User

VB. NET transmits values by type or by reference, (byval byref) C # has an out.

Sometimes I will ask a small question during the interview. Hey hey, check if you are familiar with C # basic syntax.

For example

Public void XXX (byval form as form)

And

Public void XXX (byref form as form)

What is the difference? In fact, it is very simple to correspond to C ++ is void XXX (Form * form) and void XXX (Form ** form)

The so-called pass by value and transfer by reference all have a basic problem: Who's value and who's address.

In the preceding two methods, the first value is the address of the form object. So in the first method, you write

Form. xxx = ZZZ // you can change the value of the original form object.

Form = new form ()

Form. xxx = YYY;

In fact, the original form value is not affected.

The second one is different. It passes the address, so he can change the original form value.

It is even more strange to put this problem in WebService.

[Webmethod ()]
Public void XXX (byval P as person)

[Webmethod ()]
Public void YYY (byref P as person)

We all say that WebService is stateless, so what is the difference between the first method and the second method? If the first method is similarCodeCan he affect the status of the object that the client sends to him? What about the second one?

Looking at Microsoft's implementation, we found that he had done a lot of weird code.

Look at the WSDL of his second method. It is clear that the return is void, and he should return an object for you.

<Soap: Body>
<Helloworldpersonresponse xmlns = "http://tempuri.org/">
<P>
<Name>String</Name>
</P>
</Helloworldpersonresponse>
</Soap: Body>

Then what operations does the client proxy do? Haha. The following code

Public sub helloworldperson (byref P as person)
Dim results () as object = me. Invoke ("yyy", new object () {p })
P = ctype (results (0), Person)
End sub

Ms is so trampled on the standard, huh, huh

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.