C# 函數動態配置記憶體 / 傳送空指標配置記憶體(Passing array as OUT parameter in C#)
C# 函數動態配置記憶體 / 傳送空指標配置記憶體(Passing array as OUT parameter in C#)
資料來源: https://forgetcode.com/csharp/1667-passing-array-as-out-parameter
Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
namespace ForgetCode
{
class TestOut
{
static public void FillArray(out int[] myArray)
{
// Initialize the array:
myArray = new int[5] { 1, 2, 3, 4, 5 };
}
static public void Main()
{
int[] myArray; // Initialization is not required
// Pass the array using out:
FillArray(out myArray);
// Display the array elements:
Console.WriteLine("Array elements are:");
for (int i = 0; i < myArray.Length; i++)
Console.WriteLine(myArray[i]);
}
}
}
/*
Array elements are:
1
2
3
4
5
*/