Use out and ref in C #

Source: Internet
Author: User

1. The out field must be assigned a value to the method, and the out keyword must be added before the variable to focus on the output.

2. When calling the ref modifier, you must add the ref keyword before the variable. You can modify the value or not to modify it.

3. The out and ref parameters can only pass variables, but cannot pass constants. Instead of passing the value of a variable, the address of the variable is passed.

3. The out value can be left unspecified before being passed, because the out value will definitely be assigned in the method, and the ref must have a value before being passed, because the parameter values may be used in the method.

TestOut (=; TestRef (+ =; Main (I = [] arr = {, min = max, ++ GetMaxAndMin ([] arr, max, (I =; I <arr. length-; I ++ (j =; j <arr. length -- I; j ++ (arr [j]> arr [j + temp = arr [j ++] = arr [arr. length-= arr [View Code

 

Figure (understanding that out and ref are just the address passed, not the value ):

 

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.