Marshal.WriteInt64 Method

Definition

Writes a 64-bit signed integer value to unmanaged memory. Writing to unaligned memory locations is supported.

Overloads

WriteInt64(Object, Int32, Int64)
Obsolete.

Writes a 64-bit signed integer value to unmanaged memory at a specified offset.

WriteInt64(IntPtr, Int64)

Writes a 64-bit signed integer value to unmanaged memory.

WriteInt64(IntPtr, Int32, Int64)

Writes a 64-bit signed integer value to unmanaged memory at a specified offset.

WriteInt64(Object, Int32, Int64)

Source:
Marshal.CoreCLR.cs
Source:
Marshal.CoreCLR.cs
Source:
Marshal.CoreCLR.cs

Caution

WriteInt64(Object, Int32, Int64) may be unavailable in future releases.

Writes a 64-bit signed integer value to unmanaged memory at a specified offset.

C#
[System.Obsolete("WriteInt64(Object, Int32, Int64) may be unavailable in future releases.")]
[System.Security.SecurityCritical]
public static void WriteInt64(object ptr, int ofs, long val);
C#
[System.Obsolete("WriteInt64(Object, Int32, Int64) may be unavailable in future releases.")]
public static void WriteInt64(object ptr, int ofs, long val);
C#
public static void WriteInt64(object ptr, int ofs, long val);
C#
[System.Security.SecurityCritical]
public static void WriteInt64(object ptr, int ofs, long val);

Parameters

ptr
Object

The base address in unmanaged memory of the target object.

ofs
Int32

An additional byte offset, which is added to the ptr parameter before writing.

val
Int64

The value to write.

Attributes

Exceptions

Base address (ptr) plus offset byte (ofs) produces a null or invalid address.

ptr is an ArrayWithOffset object. This method does not accept ArrayWithOffset parameters.

Remarks

WriteInt64 enables direct interaction with an unmanaged 64-bit signed array, eliminating the expense of copying an entire unmanaged array (using Marshal.Copy) to a separate managed array before setting its element values.

Writing to unaligned memory locations is supported.

See also

Applies to

.NET 9 and other versions
Product Versions (Obsolete)
.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)
.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 (4.7 (package-provided), 4.7.1 (package-provided), 4.7.2 (package-provided), 4.8 (package-provided))
.NET Standard 1.1, 2.0 (1.2, 1.3, 1.4, 1.5, 1.6, 2.1)
UWP (10.0)

WriteInt64(IntPtr, Int64)

Source:
Marshal.cs
Source:
Marshal.cs
Source:
Marshal.cs

Writes a 64-bit signed integer value to unmanaged memory.

C#
[System.Security.SecurityCritical]
public static void WriteInt64(IntPtr ptr, long val);
C#
public static void WriteInt64(IntPtr ptr, long val);

Parameters

ptr
IntPtr

The address in unmanaged memory to write to.

val
Int64

The value to write.

Attributes

Exceptions

ptr is not a recognized format.

-or-

ptr is null.

-or-

ptr is invalid.

Examples

The following example demonstrates how to read and write to an unmanaged array using the ReadInt64 and WriteInt64 methods.

C#
static void ReadWriteInt64()
{
    // Allocate unmanaged memory. 
    int elementSize = 8;
    IntPtr unmanagedArray = Marshal.AllocHGlobal(10 * elementSize);

    // Set the 10 elements of the C-style unmanagedArray
    for (int i = 0; i < 10; i++)
    {
        Marshal.WriteInt64(unmanagedArray, i * elementSize, ((Int64)(i + 1)));
    }
    Console.WriteLine("Unmanaged memory written.");

    Console.WriteLine("Reading unmanaged memory:");
    // Print the 10 elements of the C-style unmanagedArray
    for (int i = 0; i < 10; i++)
    {
        Console.WriteLine(Marshal.ReadInt64(unmanagedArray, i * elementSize));
    }

    Marshal.FreeHGlobal(unmanagedArray);

    Console.WriteLine("Done. Press Enter to continue.");
    Console.ReadLine();
}

Remarks

WriteInt64 enables direct interaction with an unmanaged 64-bit signed array, eliminating the expense of copying an entire unmanaged array (using Marshal.Copy) to a separate managed array before setting its element values.

Writing to unaligned memory locations is supported.

See also

Applies to

.NET 9 and other versions
Product Versions
.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
.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

WriteInt64(IntPtr, Int32, Int64)

Source:
Marshal.cs
Source:
Marshal.cs
Source:
Marshal.cs

Writes a 64-bit signed integer value to unmanaged memory at a specified offset.

C#
[System.Security.SecurityCritical]
public static void WriteInt64(IntPtr ptr, int ofs, long val);
C#
public static void WriteInt64(IntPtr ptr, int ofs, long val);

Parameters

ptr
IntPtr

The base address in unmanaged memory to write.

ofs
Int32

An additional byte offset, which is added to the ptr parameter before writing.

val
Int64

The value to write.

Attributes

Exceptions

Base address (ptr) plus offset byte (ofs) produces a null or invalid address.

Examples

The following example demonstrates how to read and write to an unmanaged array using the ReadInt64 and WriteInt64 methods.

C#
static void ReadWriteInt64()
{
    // Allocate unmanaged memory. 
    int elementSize = 8;
    IntPtr unmanagedArray = Marshal.AllocHGlobal(10 * elementSize);

    // Set the 10 elements of the C-style unmanagedArray
    for (int i = 0; i < 10; i++)
    {
        Marshal.WriteInt64(unmanagedArray, i * elementSize, ((Int64)(i + 1)));
    }
    Console.WriteLine("Unmanaged memory written.");

    Console.WriteLine("Reading unmanaged memory:");
    // Print the 10 elements of the C-style unmanagedArray
    for (int i = 0; i < 10; i++)
    {
        Console.WriteLine(Marshal.ReadInt64(unmanagedArray, i * elementSize));
    }

    Marshal.FreeHGlobal(unmanagedArray);

    Console.WriteLine("Done. Press Enter to continue.");
    Console.ReadLine();
}

Remarks

WriteInt64 enables direct interaction with an unmanaged 64-bit signed array, eliminating the expense of copying an entire unmanaged array (using Marshal.Copy) to a separate managed array before setting its element values.

Writing to unaligned memory locations is supported.

See also

Applies to

.NET 9 and other versions
Product Versions
.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
.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