Preface
I. out parameters
Ii. Note:
I. out parameters
The out keyword is similar to the ref keyword. The out keyword also leads to parameter passing through references. Unlike the ref keyword, the ref keyword requires that the variable be initialized before being passed, the out keyword does not require variables to be initialized before being passed, but must be assigned a value to the variable in the method. If the out keyword is used, the out keyword must be displayed for both the method definition and the method to be called.
Ii. Note:
1: If the out parameter is not assigned a value in the method body, compilation will fail.
When will the out parameter be used?
When passing a parameter in a method, the out keyword is used to indicate that the variable must return a value. For example, a division method can get both the quotient and remainder, but a common method can only return one value, in this case, the out parameter can be used to return another value. Of course, in addition to this method, the returned value is an array or multiple values can be returned.
Instance
Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Text;
Namespace _ 6_out
{
Class Program
{
Static void Main (string [] args)
{
Int I = 15;
Int j = 6;
Int yushu;
Person person = new Person ();
Console. writeLine ("{0}/{1} = {2} -- {3}", I, j, person. getShangAndYu (I, j, out yushu), yushu );
Console. ReadKey ();
}
}
Class Person
{
Public int GetShangAndYu (int I, int j, out int yushu)
{
Yushu = I % j;
Return I/j;
}
}
}
Running Effect