About the ref and out keywords [sorting]

Source: Internet
Author: User

RefThe keyword can convert the input parameter to the reference type (int is a value type, but it is converted to the reference type after ref is added, and ref must be added before the parameter is called when the function is called)

OutThe keyword is the same as the ref keyword. However, the ref parameter must be initialized before being passed, but the out parameter must be assigned a value in the function (that is, the passed parameter may not be initialized, however, you must assign values to this parameter)

Eg: If you want to determine whether a character is of the integer type, you can write it like this (by the way, the usage of TryParse ):

Protected string DigResult (string input)
{
// Define an integer variable. You can skip initialization.
Int result;
// Call int. TryParse () to determine whether the input is an integer. If so, assign the input to the result.
// When the function is executed, the result has been assigned a value. Therefore, out does not need to be initialized when the variable is defined.
If (int. TryParse (input, out result ))
{
Return result;
}
Else
{
Return "";
}
}

 

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.