Transfer of c#-function parameters __ function

Source: Internet
Author: User

As we all know, the method of parameter passing in C # functions is passed by value and passed by reference, so what's the difference between the two ways?

parameter types in C # have value types (such as int) and reference types (for example, string)

The same parameter passes are passed by value and passed by reference

We can use 22 combinations to get 4 ways to deliver:

(1) Passing value types by value

(2) Passing reference types by value

(3) Passing value types by reference

(4) Passing reference types by reference

In general, parameters are passed by value unless a particular keyword (ref and out) is used.

"Advantages and disadvantages of passing by value"

Passing by value passes a copy. One advantage of passing replicas is that you can avoid misoperation and affect the original values. The reason is that in the body of the function being called, the value of the replica is manipulated, not the original value. Of course, the transfer copy is also flawed, the most prominent should be due to duplication of performance loss, this is particularly important in large value types.

So why is the default behavior of C # 's compiler not using passing arguments by reference? is to fear that the function mistakenly manipulated the original value. This should be similar to the C # compiler requirement to display the use of keywords (ref and out), all in order to clearly express the intent to use to avoid misoperation. Using keywords such as ref, implies that the function caller knows that in the function body there may be a statement that modifies the original value, changing the value of the parameter (or state).

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.