Use the out keyword to let a method return multiple values
Source: Internet
Author: User
First, a function is defined as follows:
Public bool checkuser (string username, out string password, out int state)
{
If (username = "123 ")
{
Password = "456 ";
State = 0;
}
Else
{
Password = "";
State = 1;
}
Return false;
}
The application method is as follows:
String Password = "";
Int state = 0;
String username = "ABC ";
Bool falg = checkuser (username, out password, out State );
The three values returned by the function checkuser are falg, password, and state.
When using out parameters, you must declare them in advance. Otherwise, they cannot be used and can be considered as initialization of output parameters.
There are two methods: 1. use Pointer variables to declare functions (or use array variables) 2. the first method to use outgoing parameters: the function returns a pointer address (array address), which stores multiple variables. I am not very familiar with this method. I am often confused when passing values through addresses. Method 2: use an example. First, a function is defined as public bool checkuser (string username, out string password, out int state) {If (username = "123") {Password = "456 "; state = 0;} else {Password = ""; State = 1;} return false;} the application method is as follows: String Password = ""; int state = 0; string username = "ABC"; bool falg = checkuser (username, out password, out State ); the three values falg, password, and state returned by the function checkuser must be declared in advance when the out parameter is used. Otherwise, they cannot be used. Initialize the output parameters.
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