C#一個辦法前往多個值示例。本站提示廣大學習愛好者:(C#一個辦法前往多個值示例)文章只能為提供參考,不一定能成為您想要的結果。以下是C#一個辦法前往多個值示例正文
static void Main(string[] args)
{
//聲明
int value;
string strOutValue;
//挪用函數
//函數的參數有兩個前往的值
ReturnMulValue(out strOutValue,out value);
Console.WriteLine("Call ReturnMulValue(out string strOut, out int i)");
Console.WriteLine("value = {0:N}", value);//value = 1.00
Console.WriteLine("Out string value = " + strOutValue); //Out string value = out function
//挪用函數 http://www.cnblogs.com/roucheng/
//函數原前往值與參數中均獲得前往的值
value = ReturnMulValue(out strOutValue);
Console.WriteLine("Call ReturnMulValue(out string strOut)");
Console.WriteLine("value = {0:N}", value);//value = 2.00
Console.WriteLine("Out string value = " + strOutValue);//Out string value = out function 2
//聲明
int[] outArr;
//挪用函數
//函數前往數組
ReturnMulArr(out outArr, out value);
Console.WriteLine("Call ReturnMulArr(out int[] arr, out int i)");
Console.WriteLine("value = {0:N}", value);//value = 3.00
Console.WriteLine("Out arr value ");
for (int j = 0; j < outArr.Length; j++)
{
Console.Write(outArr[j]);//12345
}
}
/// <summary>
/// 前往多個值,原函數前往空
/// </summary>
/// <param name="strOut">應用out症結字前往的字符串</param>
/// <param name="i">應用out症結字前往的數值</param>
static void ReturnMulValue(out string strOut, out int i)
{
i = 1;
strOut = "out function";
}
/// <summary>
/// 前往多個值,原函數前往數值
/// </summary>
/// <param name="strOut">應用out症結字前往的字符串</param>
/// <returns>原函數直接前往的數值</returns>
static int ReturnMulValue(out string strOut)
{
int i = 2;
strOut = "out function 2";
return i;
}
/// <summary>
/// 前往數組
/// </summary>
/// <param name="arr">應用out症結字前往的數組</param>
/// <param name="i">應用out症結字前往的數值</param>
static void ReturnMulArr(out int[] arr, out int i)
{
arr = new int[5] { 1, 2, 3, 4, 5};
i = 3;
}