Marshal.Copy 方法

定義

從 Managed 陣列複製資料到 Unmanaged 記憶體指標,或從 Unmanaged 記憶體指標複製資料到 Managed 陣列。

多載

Copy(Single[], Int32, IntPtr, Int32)

從一維、Managed 單精確度浮點數陣列將資料複製到 Unmanaged 記憶體指標。

Copy(IntPtr, Single[], Int32, Int32)

從 Unmanaged 記憶體指標將資料複製到 Managed 單精確度浮點數陣列。

Copy(IntPtr, IntPtr[], Int32, Int32)

從 Unmanaged 記憶體指標,將資料複製到 Managed IntPtr 陣列。

Copy(IntPtr, Int64[], Int32, Int32)

從 Unmanaged 記憶體指標將資料複製到 Managed 64 位元帶正負號的整數陣列。

Copy(IntPtr, Int32[], Int32, Int32)

從 Unmanaged 記憶體指標將資料複製到 Managed 32 位元帶正負號的整數陣列。

Copy(IntPtr, Int16[], Int32, Int32)

從 Unmanaged 記憶體指標將資料複製到 Managed 16 位元帶正負號的整數陣列。

Copy(IntPtr, Double[], Int32, Int32)

從 Unmanaged 記憶體指標將資料複製到Managed 雙精確度浮點數陣列。

Copy(IntPtr[], Int32, IntPtr, Int32)

從一維、Managed IntPtr 陣列將資料複製到 Unmanaged 記憶體指標。

Copy(IntPtr, Byte[], Int32, Int32)

從 Unmanaged 記憶體指標將資料複製到 Managed 8 位元不帶正負號的整數 (Unsigned Integer) 陣列。

Copy(Int64[], Int32, IntPtr, Int32)

從一維、Managed 64 位元帶正負號的整數陣列將資料複製到 Unmanaged 記憶體指標。

Copy(Int32[], Int32, IntPtr, Int32)

從一維、Managed 32 位元帶正負號的整數陣列將資料複製到 Unmanaged 記憶體指標。

Copy(Int16[], Int32, IntPtr, Int32)

從一維、Managed 16 位元帶正負號的整數陣列將資料複製到 Unmanaged 記憶體指標。

Copy(Double[], Int32, IntPtr, Int32)

從一維、Managed 雙精確度浮點數陣列將資料複製到 Unmanaged 記憶體指標。

Copy(Char[], Int32, IntPtr, Int32)

從一維、Managed 字元陣列將資料複製到 Unmanaged 記憶體指標。

Copy(IntPtr, Char[], Int32, Int32)

從 Unmanaged 記憶體指標將資料複製到 Managed 字元陣列。

Copy(Byte[], Int32, IntPtr, Int32)

從一維、Managed 8 位元不帶正負號的整數 (Unsigned Integer) 陣列將資料複製到 Unmanaged 記憶體指標。

Copy(Single[], Int32, IntPtr, Int32)

來源:
Marshal.cs
來源:
Marshal.cs
來源:
Marshal.cs

從一維、Managed 單精確度浮點數陣列將資料複製到 Unmanaged 記憶體指標。

C#
[System.Security.SecurityCritical]
public static void Copy(float[] source, int startIndex, IntPtr destination, int length);
C#
public static void Copy(float[] source, int startIndex, IntPtr destination, int length);

參數

source
Single[]

要複製的一維陣列。

startIndex
Int32

複製應該在此處開始之來源陣列中以零起始的索引。

destination
IntPtr

要複製到的記憶體指標。

length
Int32

要複製的陣列元素數目。

屬性

例外狀況

startIndexlength 無效。

sourcestartIndexdestinationlengthnull

備註

您可以使用這個方法,將一維 Managed 陣列的子集複製到 Unmanaged C 樣式陣列。

適用於

.NET 10 和其他版本
產品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

Copy(IntPtr, Single[], Int32, Int32)

來源:
Marshal.cs
來源:
Marshal.cs
來源:
Marshal.cs

從 Unmanaged 記憶體指標將資料複製到 Managed 單精確度浮點數陣列。

C#
[System.Security.SecurityCritical]
public static void Copy(IntPtr source, float[] destination, int startIndex, int length);
C#
public static void Copy(IntPtr source, float[] destination, int startIndex, int length);

參數

source
IntPtr

要複製的來源記憶體指標。

destination
Single[]

要複製到其中的陣列。

startIndex
Int32

複製應該在此處開始之目的地陣列中以零起始的索引。

length
Int32

要複製的陣列元素數目。

屬性

例外狀況

sourcedestinationstartIndexlengthnull

備註

Unmanaged、C 樣式數位不包含界限資訊,這可防止 startIndex 驗證 和 length 參數。 因此,對應至 參數的 source Unmanaged 數據會填入 Managed 陣列,而不論其有用性為何。 您必須先使用適當的大小初始化 Managed 陣列,才能呼叫這個方法。

另請參閱

適用於

.NET 10 和其他版本
產品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

Copy(IntPtr, IntPtr[], Int32, Int32)

來源:
Marshal.cs
來源:
Marshal.cs
來源:
Marshal.cs

從 Unmanaged 記憶體指標,將資料複製到 Managed IntPtr 陣列。

C#
[System.Security.SecurityCritical]
public static void Copy(IntPtr source, IntPtr[] destination, int startIndex, int length);
C#
public static void Copy(IntPtr source, IntPtr[] destination, int startIndex, int length);

參數

source
IntPtr

要複製的來源記憶體指標。

destination

IntPtr[]

要複製到其中的陣列。

startIndex
Int32

複製應該在此處開始之目的地陣列中以零起始的索引。

length
Int32

要複製的陣列元素數目。

屬性

例外狀況

sourcedestinationstartIndexlengthnull

備註

Unmanaged、C 樣式數位不包含界限資訊,這可防止 startIndex 驗證 和 length 參數。 因此,對應至 source 參數的 Unmanaged 數據會填入 Managed 陣列,而不論其有用性為何。 您必須先使用適當的大小初始化 Managed 陣列,才能呼叫 Marshal.Copy 方法。

適用於

.NET 10 和其他版本
產品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

Copy(IntPtr, Int64[], Int32, Int32)

來源:
Marshal.cs
來源:
Marshal.cs
來源:
Marshal.cs

從 Unmanaged 記憶體指標將資料複製到 Managed 64 位元帶正負號的整數陣列。

C#
[System.Security.SecurityCritical]
public static void Copy(IntPtr source, long[] destination, int startIndex, int length);
C#
public static void Copy(IntPtr source, long[] destination, int startIndex, int length);

參數

source
IntPtr

要複製的來源記憶體指標。

destination
Int64[]

要複製到其中的陣列。

startIndex
Int32

複製應該在此處開始之目的地陣列中以零起始的索引。

length
Int32

要複製的陣列元素數目。

屬性

例外狀況

sourcedestinationstartIndexlengthnull

範例

下列範例會將陣列複製到 Unmanaged 記憶體,然後將 Unmanaged 陣組複製到 Managed 記憶體。

C#
using System;
using System.Runtime.InteropServices;

class Example
{

    static void Main()
    {
        // Create a managed array.
        Int64[] managedArray = { 1, 2, 3, 4 };

        // Initialize unmanaged memory to hold the array.
        int size = Marshal.SizeOf(managedArray[0]) * managedArray.Length;

        IntPtr pnt = Marshal.AllocHGlobal(size);

        try
        {
            // Copy the array to unmanaged memory.
            Marshal.Copy(managedArray, 0, pnt, managedArray.Length);

            // Copy the unmanaged array back to another managed array.

            Int64[] managedArray2 = new Int64[managedArray.Length];

            Marshal.Copy(pnt, managedArray2, 0, managedArray.Length);

            Console.WriteLine("The array was copied to unmanaged memory and back.");
        }
        finally
        {
            // Free the unmanaged memory.
            Marshal.FreeHGlobal(pnt);
        }
    }
}

備註

Unmanaged、C 樣式數位不包含界限資訊,這可防止 startIndex 驗證 和 length 參數。 因此,對應至 參數的source Unmanaged 數據會填入 Managed 陣列,而不論其有用性為何。 您必須先使用適當的大小初始化 Managed 陣列,才能呼叫這個方法。

另請參閱

適用於

.NET 10 和其他版本
產品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

Copy(IntPtr, Int32[], Int32, Int32)

來源:
Marshal.cs
來源:
Marshal.cs
來源:
Marshal.cs

從 Unmanaged 記憶體指標將資料複製到 Managed 32 位元帶正負號的整數陣列。

C#
[System.Security.SecurityCritical]
public static void Copy(IntPtr source, int[] destination, int startIndex, int length);
C#
public static void Copy(IntPtr source, int[] destination, int startIndex, int length);

參數

source
IntPtr

要複製的來源記憶體指標。

destination
Int32[]

要複製到其中的陣列。

startIndex
Int32

複製應該在此處開始之目的地陣列中以零起始的索引。

length
Int32

要複製的陣列元素數目。

屬性

例外狀況

sourcedestinationstartIndexlengthnull

範例

下列範例會將陣列複製到 Unmanaged 記憶體,然後將 Unmanaged 陣組複製到 Managed 記憶體。

C#
using System;
using System.Runtime.InteropServices;

class Example
{

    static void Main()
    {
        // Create a managed array.
        int[] managedArray = { 1, 2, 3, 4 };

        // Initialize unmanaged memory to hold the array.
        int size = Marshal.SizeOf(managedArray[0]) * managedArray.Length;

        IntPtr pnt = Marshal.AllocHGlobal(size);

        try
        {
            // Copy the array to unmanaged memory.
            Marshal.Copy(managedArray, 0, pnt, managedArray.Length);

            // Copy the unmanaged array back to another managed array.

            int[] managedArray2 = new int[managedArray.Length];

            Marshal.Copy(pnt, managedArray2, 0, managedArray.Length);

            Console.WriteLine("The array was copied to unmanaged memory and back.");
        }
        finally
        {
            // Free the unmanaged memory.
            Marshal.FreeHGlobal(pnt);
        }
    }
}

備註

Unmanaged、C 樣式數位不包含界限資訊,這可防止 startIndex 驗證 和 length 參數。 因此,對應至 參數的 source Unmanaged 數據會填入 Managed 陣列,而不論其有用性為何。 您必須先使用適當的大小初始化 Managed 陣列,才能呼叫這個方法。

另請參閱

適用於

.NET 10 和其他版本
產品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

Copy(IntPtr, Int16[], Int32, Int32)

來源:
Marshal.cs
來源:
Marshal.cs
來源:
Marshal.cs

從 Unmanaged 記憶體指標將資料複製到 Managed 16 位元帶正負號的整數陣列。

C#
[System.Security.SecurityCritical]
public static void Copy(IntPtr source, short[] destination, int startIndex, int length);
C#
public static void Copy(IntPtr source, short[] destination, int startIndex, int length);

參數

source
IntPtr

要複製的來源記憶體指標。

destination
Int16[]

要複製到其中的陣列。

startIndex
Int32

複製應該在此處開始之目的地陣列中以零起始的索引。

length
Int32

要複製的陣列元素數目。

屬性

例外狀況

sourcedestinationstartIndexlengthnull

範例

下列範例會將陣列複製到 Unmanaged 記憶體,然後將 Unmanaged 陣組複製到 Managed 記憶體。

C#
using System;
using System.Runtime.InteropServices;

class Example
{

    static void Main()
    {
        // Create a managed array.
        short[] managedArray = { 1, 2, 3, 4 };

        // Initialize unmanaged memory to hold the array.
        int size = Marshal.SizeOf(managedArray[0]) * managedArray.Length;

        IntPtr pnt = Marshal.AllocHGlobal(size);

        try
        {
            // Copy the array to unmanaged memory.
            Marshal.Copy(managedArray, 0, pnt, managedArray.Length);

            // Copy the unmanaged array back to another managed array.

            short[] managedArray2 = new short[managedArray.Length];

            Marshal.Copy(pnt, managedArray2, 0, managedArray.Length);

            Console.WriteLine("The array was copied to unmanaged memory and back.");
        }
        finally
        {
            // Free the unmanaged memory.
            Marshal.FreeHGlobal(pnt);
        }
    }
}

備註

Unmanaged、C 樣式數位不包含界限資訊,這可防止 startIndex 驗證 和 length 參數。 因此,對應至 參數的 source Unmanaged 數據會填入 Managed 陣列,而不論其有用性為何。 您必須先使用適當的大小初始化 Managed 陣列,才能呼叫這個方法。

另請參閱

適用於

.NET 10 和其他版本
產品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

Copy(IntPtr, Double[], Int32, Int32)

來源:
Marshal.cs
來源:
Marshal.cs
來源:
Marshal.cs

從 Unmanaged 記憶體指標將資料複製到Managed 雙精確度浮點數陣列。

C#
[System.Security.SecurityCritical]
public static void Copy(IntPtr source, double[] destination, int startIndex, int length);
C#
public static void Copy(IntPtr source, double[] destination, int startIndex, int length);

參數

source
IntPtr

要複製的來源記憶體指標。

destination
Double[]

要複製到其中的陣列。

startIndex
Int32

複製應該在此處開始之目的地陣列中以零起始的索引。

length
Int32

要複製的陣列元素數目。

屬性

例外狀況

sourcedestinationstartIndexlengthnull

範例

下列範例會將陣列複製到 Unmanaged 記憶體,然後將 Unmanaged 陣組複製到 Managed 記憶體。

C#
using System;
using System.Runtime.InteropServices;

class Example
{

    static void Main()
    {
        // Create a managed array.
        double[] managedArray = { 0.1, 0.2, 0.3, 0.4 };

        // Initialize unmanaged memory to hold the array.
        int size = Marshal.SizeOf(managedArray[0]) * managedArray.Length;

        IntPtr pnt = Marshal.AllocHGlobal(size);

        try
        {
            // Copy the array to unmanaged memory.
            Marshal.Copy(managedArray, 0, pnt, managedArray.Length);

            // Copy the unmanaged array back to another managed array.

            double[] managedArray2 = new double[managedArray.Length];

            Marshal.Copy(pnt, managedArray2, 0, managedArray.Length);

            Console.WriteLine("The array was copied to unmanaged memory and back.");
        }
        finally
        {
            // Free the unmanaged memory.
            Marshal.FreeHGlobal(pnt);
        }
    }
}

備註

Unmanaged、C 樣式數位不包含界限資訊,這可防止 startIndex 驗證 和 length 參數。 因此,對應至 參數的 source Unmanaged 數據會填入 Managed 陣列,而不論其有用性為何。 您必須先使用適當的大小初始化 Managed 陣列,才能呼叫這個方法。

另請參閱

適用於

.NET 10 和其他版本
產品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

Copy(IntPtr[], Int32, IntPtr, Int32)

來源:
Marshal.cs
來源:
Marshal.cs
來源:
Marshal.cs

從一維、Managed IntPtr 陣列將資料複製到 Unmanaged 記憶體指標。

C#
[System.Security.SecurityCritical]
public static void Copy(IntPtr[] source, int startIndex, IntPtr destination, int length);
C#
public static void Copy(IntPtr[] source, int startIndex, IntPtr destination, int length);

參數

source

IntPtr[]

要複製的一維陣列。

startIndex
Int32

複製應該在此處開始之來源陣列中以零起始的索引。

destination
IntPtr

要複製到的記憶體指標。

length
Int32

要複製的陣列元素數目。

屬性

例外狀況

sourcedestinationstartIndexlengthnull

備註

您可以使用這個方法,將一維 Managed IntPtr 陣列的子集複製到 Unmanaged C 樣式陣列。

適用於

.NET 10 和其他版本
產品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

Copy(IntPtr, Byte[], Int32, Int32)

來源:
Marshal.cs
來源:
Marshal.cs
來源:
Marshal.cs

從 Unmanaged 記憶體指標將資料複製到 Managed 8 位元不帶正負號的整數 (Unsigned Integer) 陣列。

C#
[System.Security.SecurityCritical]
public static void Copy(IntPtr source, byte[] destination, int startIndex, int length);
C#
public static void Copy(IntPtr source, byte[] destination, int startIndex, int length);

參數

source
IntPtr

要複製的來源記憶體指標。

destination
Byte[]

要複製到其中的陣列。

startIndex
Int32

複製應該在此處開始之目的地陣列中以零起始的索引。

length
Int32

要複製的陣列元素數目。

屬性

例外狀況

sourcedestinationstartIndexlengthnull

範例

下列範例會將陣列複製到 Unmanaged 記憶體,然後將 Unmanaged 陣組複製到 Managed 記憶體。

C#
using System;
using System.Runtime.InteropServices;

class Example
{

    static void Main()
    {
        // Create a managed array.
        byte[] managedArray = { 1, 2, 3, 4 };

        // Initialize unmanaged memory to hold the array.
        int size = Marshal.SizeOf(managedArray[0]) * managedArray.Length;

        IntPtr pnt = Marshal.AllocHGlobal(size);

        try
        {
            // Copy the array to unmanaged memory.
            Marshal.Copy(managedArray, 0, pnt, managedArray.Length);

            // Copy the unmanaged array back to another managed array.

            byte[] managedArray2 = new byte[managedArray.Length];

            Marshal.Copy(pnt, managedArray2, 0, managedArray.Length);

            Console.WriteLine("The array was copied to unmanaged memory and back.");
        }
        finally
        {
            // Free the unmanaged memory.
            Marshal.FreeHGlobal(pnt);
        }
    }
}

備註

Unmanaged、C 樣式數位不包含界限資訊,這可防止 startIndex 驗證 和 length 參數。 因此,對應至 參數的 source Unmanaged 數據會填入 Managed 陣列,而不論其有用性為何。 您必須先使用適當的大小初始化 Managed 陣列,才能呼叫這個方法。

另請參閱

適用於

.NET 10 和其他版本
產品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

Copy(Int64[], Int32, IntPtr, Int32)

來源:
Marshal.cs
來源:
Marshal.cs
來源:
Marshal.cs

從一維、Managed 64 位元帶正負號的整數陣列將資料複製到 Unmanaged 記憶體指標。

C#
[System.Security.SecurityCritical]
public static void Copy(long[] source, int startIndex, IntPtr destination, int length);
C#
public static void Copy(long[] source, int startIndex, IntPtr destination, int length);

參數

source
Int64[]

要複製的一維陣列。

startIndex
Int32

複製應該在此處開始之來源陣列中以零起始的索引。

destination
IntPtr

要複製到的記憶體指標。

length
Int32

要複製的陣列元素數目。

屬性

例外狀況

startIndexlength 無效。

sourcestartIndexdestinationlengthnull

範例

下列範例會將陣列複製到 Unmanaged 記憶體,然後將 Unmanaged 陣組複製到 Managed 記憶體。

C#
using System;
using System.Runtime.InteropServices;

class Example
{

    static void Main()
    {
        // Create a managed array.
        Int64[] managedArray = { 1, 2, 3, 4 };

        // Initialize unmanaged memory to hold the array.
        int size = Marshal.SizeOf(managedArray[0]) * managedArray.Length;

        IntPtr pnt = Marshal.AllocHGlobal(size);

        try
        {
            // Copy the array to unmanaged memory.
            Marshal.Copy(managedArray, 0, pnt, managedArray.Length);

            // Copy the unmanaged array back to another managed array.

            Int64[] managedArray2 = new Int64[managedArray.Length];

            Marshal.Copy(pnt, managedArray2, 0, managedArray.Length);

            Console.WriteLine("The array was copied to unmanaged memory and back.");
        }
        finally
        {
            // Free the unmanaged memory.
            Marshal.FreeHGlobal(pnt);
        }
    }
}

備註

您可以使用這個方法,將一維 Managed 陣列的子集複製到 Unmanaged C 樣式陣列。

適用於

.NET 10 和其他版本
產品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

Copy(Int32[], Int32, IntPtr, Int32)

來源:
Marshal.cs
來源:
Marshal.cs
來源:
Marshal.cs

從一維、Managed 32 位元帶正負號的整數陣列將資料複製到 Unmanaged 記憶體指標。

C#
[System.Security.SecurityCritical]
public static void Copy(int[] source, int startIndex, IntPtr destination, int length);
C#
public static void Copy(int[] source, int startIndex, IntPtr destination, int length);

參數

source
Int32[]

要複製的一維陣列。

startIndex
Int32

複製應該在此處開始之來源陣列中以零起始的索引。

destination
IntPtr

要複製到的記憶體指標。

length
Int32

要複製的陣列元素數目。

屬性

例外狀況

startIndexlength 無效。

startIndexlengthnull

範例

下列範例會將陣列複製到 Unmanaged 記憶體,然後將 Unmanaged 陣組複製到 Managed 記憶體。

C#
using System;
using System.Runtime.InteropServices;

class Example
{

    static void Main()
    {
        // Create a managed array.
        int[] managedArray = { 1, 2, 3, 4 };

        // Initialize unmanaged memory to hold the array.
        int size = Marshal.SizeOf(managedArray[0]) * managedArray.Length;

        IntPtr pnt = Marshal.AllocHGlobal(size);

        try
        {
            // Copy the array to unmanaged memory.
            Marshal.Copy(managedArray, 0, pnt, managedArray.Length);

            // Copy the unmanaged array back to another managed array.

            int[] managedArray2 = new int[managedArray.Length];

            Marshal.Copy(pnt, managedArray2, 0, managedArray.Length);

            Console.WriteLine("The array was copied to unmanaged memory and back.");
        }
        finally
        {
            // Free the unmanaged memory.
            Marshal.FreeHGlobal(pnt);
        }
    }
}

備註

您可以使用這個方法,將一維 Managed 陣列的子集複製到 Unmanaged C 樣式陣列。

適用於

.NET 10 和其他版本
產品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

Copy(Int16[], Int32, IntPtr, Int32)

來源:
Marshal.cs
來源:
Marshal.cs
來源:
Marshal.cs

從一維、Managed 16 位元帶正負號的整數陣列將資料複製到 Unmanaged 記憶體指標。

C#
[System.Security.SecurityCritical]
public static void Copy(short[] source, int startIndex, IntPtr destination, int length);
C#
public static void Copy(short[] source, int startIndex, IntPtr destination, int length);

參數

source
Int16[]

要複製的一維陣列。

startIndex
Int32

複製應該在此處開始之來源陣列中以零起始的索引。

destination
IntPtr

要複製到的記憶體指標。

length
Int32

要複製的陣列元素數目。

屬性

例外狀況

startIndexlength 無效。

sourcestartIndexdestinationlengthnull

範例

下列範例會將陣列複製到 Unmanaged 記憶體,然後將 Unmanaged 陣組複製到 Managed 記憶體。

C#
using System;
using System.Runtime.InteropServices;

class Example
{

    static void Main()
    {
        // Create a managed array.
        short[] managedArray = { 1, 2, 3, 4 };

        // Initialize unmanaged memory to hold the array.
        int size = Marshal.SizeOf(managedArray[0]) * managedArray.Length;

        IntPtr pnt = Marshal.AllocHGlobal(size);

        try
        {
            // Copy the array to unmanaged memory.
            Marshal.Copy(managedArray, 0, pnt, managedArray.Length);

            // Copy the unmanaged array back to another managed array.

            short[] managedArray2 = new short[managedArray.Length];

            Marshal.Copy(pnt, managedArray2, 0, managedArray.Length);

            Console.WriteLine("The array was copied to unmanaged memory and back.");
        }
        finally
        {
            // Free the unmanaged memory.
            Marshal.FreeHGlobal(pnt);
        }
    }
}

備註

您可以使用這個方法,將一維 Managed 陣列的子集複製到 Unmanaged C 樣式陣列。

適用於

.NET 10 和其他版本
產品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

Copy(Double[], Int32, IntPtr, Int32)

來源:
Marshal.cs
來源:
Marshal.cs
來源:
Marshal.cs

從一維、Managed 雙精確度浮點數陣列將資料複製到 Unmanaged 記憶體指標。

C#
[System.Security.SecurityCritical]
public static void Copy(double[] source, int startIndex, IntPtr destination, int length);
C#
public static void Copy(double[] source, int startIndex, IntPtr destination, int length);

參數

source
Double[]

要複製的一維陣列。

startIndex
Int32

複製應該在此處開始之來源陣列中以零起始的索引。

destination
IntPtr

要複製到的記憶體指標。

length
Int32

要複製的陣列元素數目。

屬性

例外狀況

startIndexlength 無效。

sourcestartIndexdestinationlengthnull

範例

下列範例會將陣列複製到 Unmanaged 記憶體,然後將 Unmanaged 陣組複製到 Managed 記憶體。

C#
using System;
using System.Runtime.InteropServices;

class Example
{

    static void Main()
    {
        // Create a managed array.
        double[] managedArray = { 0.1, 0.2, 0.3, 0.4 };

        // Initialize unmanaged memory to hold the array.
        int size = Marshal.SizeOf(managedArray[0]) * managedArray.Length;

        IntPtr pnt = Marshal.AllocHGlobal(size);

        try
        {
            // Copy the array to unmanaged memory.
            Marshal.Copy(managedArray, 0, pnt, managedArray.Length);

            // Copy the unmanaged array back to another managed array.

            double[] managedArray2 = new double[managedArray.Length];

            Marshal.Copy(pnt, managedArray2, 0, managedArray.Length);

            Console.WriteLine("The array was copied to unmanaged memory and back.");
        }
        finally
        {
            // Free the unmanaged memory.
            Marshal.FreeHGlobal(pnt);
        }
    }
}

備註

您可以使用這個方法,將一維 Managed 陣列的子集複製到 Unmanaged C 樣式陣列。

適用於

.NET 10 和其他版本
產品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

Copy(Char[], Int32, IntPtr, Int32)

來源:
Marshal.cs
來源:
Marshal.cs
來源:
Marshal.cs

從一維、Managed 字元陣列將資料複製到 Unmanaged 記憶體指標。

C#
[System.Security.SecurityCritical]
public static void Copy(char[] source, int startIndex, IntPtr destination, int length);
C#
public static void Copy(char[] source, int startIndex, IntPtr destination, int length);

參數

source
Char[]

要複製的一維陣列。

startIndex
Int32

複製應該在此處開始之來源陣列中以零起始的索引。

destination
IntPtr

要複製到的記憶體指標。

length
Int32

要複製的陣列元素數目。

屬性

例外狀況

startIndexlength 無效。

startIndexdestinationlengthnull

範例

下列範例會將陣列複製到 Unmanaged 記憶體,然後將 Unmanaged 陣組複製到 Managed 記憶體。

C#
// Remember that the actual size of System.Char in unmanaged memory is 2.
using System;
using System.Runtime.InteropServices;

class Example
{

    static void Main()
    {
        // Create a managed array.
        char[] managedArray = new char[1000];
        managedArray[0] = 'a';
        managedArray[1] = 'b';
        managedArray[2] = 'c';
        managedArray[3] = 'd';
        managedArray[999] = 'Z';

        // Initialize unmanaged memory to hold the array.
        // int size = Marshal.SizeOf(managedArray[0]) * managedArray.Length;  // Incorrect
        int size = Marshal.SystemDefaultCharSize * managedArray.Length;       // Correct

        IntPtr pnt = Marshal.AllocHGlobal(size);

        try
        {
            // Copy the array to unmanaged memory.
            Marshal.Copy(managedArray, 0, pnt, managedArray.Length);

            // Copy the unmanaged array back to another managed array.

            char[] managedArray2 = new char[managedArray.Length];

            Marshal.Copy(pnt, managedArray2, 0, managedArray.Length);
            Console.WriteLine("Here is the roundtripped array: {0} {1} {2} {3} {4}",
                               managedArray2[0], managedArray2[1], managedArray2[2], managedArray2[3],
                               managedArray2[999]);

            Console.WriteLine("The array was copied to unmanaged memory and back.");
        }
        finally
        {
            // Free the unmanaged memory.
            Marshal.FreeHGlobal(pnt);
        }
    }
}

備註

您可以使用這個方法,將一維 Managed 陣列的子集複製到 Unmanaged C 樣式陣列。

適用於

.NET 10 和其他版本
產品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

Copy(IntPtr, Char[], Int32, Int32)

來源:
Marshal.cs
來源:
Marshal.cs
來源:
Marshal.cs

從 Unmanaged 記憶體指標將資料複製到 Managed 字元陣列。

C#
[System.Security.SecurityCritical]
public static void Copy(IntPtr source, char[] destination, int startIndex, int length);
C#
public static void Copy(IntPtr source, char[] destination, int startIndex, int length);

參數

source
IntPtr

要複製的來源記憶體指標。

destination
Char[]

要複製到其中的陣列。

startIndex
Int32

複製應該在此處開始之目的地陣列中以零起始的索引。

length
Int32

要複製的陣列元素數目。

屬性

例外狀況

sourcedestinationstartIndexlengthnull

範例

下列範例會將陣列複製到 Unmanaged 記憶體,然後將 Unmanaged 陣組複製到 Managed 記憶體。

C#
// Remember that the actual size of System.Char in unmanaged memory is 2.
using System;
using System.Runtime.InteropServices;

class Example
{

    static void Main()
    {
        // Create a managed array.
        char[] managedArray = new char[1000];
        managedArray[0] = 'a';
        managedArray[1] = 'b';
        managedArray[2] = 'c';
        managedArray[3] = 'd';
        managedArray[999] = 'Z';

        // Initialize unmanaged memory to hold the array.
        // int size = Marshal.SizeOf(managedArray[0]) * managedArray.Length;  // Incorrect
        int size = Marshal.SystemDefaultCharSize * managedArray.Length;       // Correct

        IntPtr pnt = Marshal.AllocHGlobal(size);

        try
        {
            // Copy the array to unmanaged memory.
            Marshal.Copy(managedArray, 0, pnt, managedArray.Length);

            // Copy the unmanaged array back to another managed array.

            char[] managedArray2 = new char[managedArray.Length];

            Marshal.Copy(pnt, managedArray2, 0, managedArray.Length);
            Console.WriteLine("Here is the roundtripped array: {0} {1} {2} {3} {4}",
                               managedArray2[0], managedArray2[1], managedArray2[2], managedArray2[3],
                               managedArray2[999]);

            Console.WriteLine("The array was copied to unmanaged memory and back.");
        }
        finally
        {
            // Free the unmanaged memory.
            Marshal.FreeHGlobal(pnt);
        }
    }
}

備註

Unmanaged、C 樣式數位不包含界限資訊,這可防止 startIndex 驗證 和 length 參數。 因此,對應至 參數的 source Unmanaged 數據會填入 Managed 陣列,而不論其有用性為何。 您必須先使用適當的大小初始化 Managed 陣列,才能呼叫這個方法。

另請參閱

適用於

.NET 10 和其他版本
產品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

Copy(Byte[], Int32, IntPtr, Int32)

來源:
Marshal.cs
來源:
Marshal.cs
來源:
Marshal.cs

從一維、Managed 8 位元不帶正負號的整數 (Unsigned Integer) 陣列將資料複製到 Unmanaged 記憶體指標。

C#
[System.Security.SecurityCritical]
public static void Copy(byte[] source, int startIndex, IntPtr destination, int length);
C#
public static void Copy(byte[] source, int startIndex, IntPtr destination, int length);

參數

source
Byte[]

要複製的一維陣列。

startIndex
Int32

複製應該在此處開始之來源陣列中以零起始的索引。

destination
IntPtr

要複製到的記憶體指標。

length
Int32

要複製的陣列元素數目。

屬性

例外狀況

startIndexlength 無效。

sourcestartIndexdestinationlengthnull

範例

下列範例會使用 多 Copy(Byte[], Int32, IntPtr, Int32) 載將數位複製到 Unmanaged 記憶體,然後使用 多載將 Unmanaged 陣列複製到 Managed 記憶體 Copy(IntPtr, Byte[], Int32, Int32)

C#
using System;
using System.Runtime.InteropServices;

class Example
{

    static void Main()
    {
        // Create a managed array.
        byte[] managedArray = { 1, 2, 3, 4 };

        // Initialize unmanaged memory to hold the array.
        int size = Marshal.SizeOf(managedArray[0]) * managedArray.Length;

        IntPtr pnt = Marshal.AllocHGlobal(size);

        try
        {
            // Copy the array to unmanaged memory.
            Marshal.Copy(managedArray, 0, pnt, managedArray.Length);

            // Copy the unmanaged array back to another managed array.

            byte[] managedArray2 = new byte[managedArray.Length];

            Marshal.Copy(pnt, managedArray2, 0, managedArray.Length);

            Console.WriteLine("The array was copied to unmanaged memory and back.");
        }
        finally
        {
            // Free the unmanaged memory.
            Marshal.FreeHGlobal(pnt);
        }
    }
}

備註

您可以使用這個方法,將一維 Managed 陣列的子集複製到 Unmanaged C 樣式陣列。

適用於

.NET 10 和其他版本
產品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0