As with the REF keyword, the Out keyword is also passed by reference.
The example shows how to use the Out keyword to get the index of the maximum and maximum values in the array
Using System;
Using System.Collections.Generic;
Using System.Text;
namespace Sampsong
{
class Program1
{
///<summary>
///The index of the maximum and maximum values of the array
///</summary>
///<param name= "M_array" > incoming array </param>
///<param name= "M_index" > maximum index required </param>
///<returns > maximum values in arrays </returns>
static int Maxindex (int[] M_array, out int m_index)
{
M_index = 0;
int m_max = m_array[0];
For (int i = 0; i < m_array.length; i++)
{
if (M_array[i] > M_max)
{
M_max = M_array[i];
M_index = i;
}
}
return M_max;
}
static void Main (string[] args)
{
int[] myarray=new int[5]{56,89,425,21,21};
int maxindex;
Console. WriteLine ("The largest value in the array is: {0}", Maxindex (MyArray, outmaxindex));
// The value of Maxindex after calling the Maxindex method is 2
Console. WriteLine ("The largest value in the array is the {0} element", maxindex+1);
}
}
}
The largest value in the array is: 425 the largest value in the array is the 3rd element, press any key to continue ...
Description
The 1.out keyword causes parameters to be passed by reference. This is similar to the ref keyword, except that the ref requires that the variable be initialized before it is passed.
2. both the method definition and the calling method must explicitly use the Out keyword.
3. The property is not a variable and therefore cannot be passed as an out parameter.
4. Example
Using System;
Using System.Collections.Generic;
Using System.Text;
namespace Sampsong
{
class Program1
{
static void Method (out string name, out int grad,out string school)
{
Name = " Song";
Grad = 2005;
School = " Zhengzhou University";
}
static void Main (string[] args)
{
String Name;
String School;
int Grad;
Method (outName, outGrad, outSchool);
Console.WriteLine ( " call method" "
Console.WriteLine (" student" + Name + " yes" + School + Grad + " level student");
}
}
}
Run results
After calling method methods
On the Out keyword in C #