Marshal.WriteInt16 Method

Definition

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

Overloads

WriteInt16(IntPtr, Char)

Writes a character as a 16-bit integer value to unmanaged memory.

WriteInt16(IntPtr, Int16)

Writes a 16-bit integer value to unmanaged memory.

WriteInt16(IntPtr, Int32, Char)

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

WriteInt16(IntPtr, Int32, Int16)

Writes a 16-bit signed integer value into unmanaged memory at a specified offset.

WriteInt16(Object, Int32, Char)
Obsolete.

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

WriteInt16(Object, Int32, Int16)
Obsolete.

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

WriteInt16(IntPtr, Char)

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

Writes a character as a 16-bit integer value to unmanaged memory.

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

Parameters

ptr
IntPtr

The address in unmanaged memory to write to.

val
Char

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 ReadInt16 and WriteInt16 methods.

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

    // Set the 10 elements of the C-style unmanagedArray
    for (int i = 0; i < 10; i++)
    {
        Marshal.WriteInt16(unmanagedArray, i * elementSize, ((Int16)(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.ReadInt16(unmanagedArray, i * elementSize));
    }

    Marshal.FreeHGlobal(unmanagedArray);

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

Remarks

WriteInt16 enables direct interaction with an unmanaged 16-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

WriteInt16(IntPtr, Int16)

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

Writes a 16-bit integer value to unmanaged memory.

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

Parameters

ptr
IntPtr

The address in unmanaged memory to write to.

val
Int16

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 ReadInt16 and WriteInt16 methods.

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

    // Set the 10 elements of the C-style unmanagedArray
    for (int i = 0; i < 10; i++)
    {
        Marshal.WriteInt16(unmanagedArray, i * elementSize, ((Int16)(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.ReadInt16(unmanagedArray, i * elementSize));
    }

    Marshal.FreeHGlobal(unmanagedArray);

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

Remarks

WriteInt16 enables direct interaction with an unmanaged 16-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

WriteInt16(IntPtr, Int32, Char)

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

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

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

Parameters

ptr
IntPtr

The base address in the native heap to write to.

ofs
Int32

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

val
Char

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 ReadInt16 and WriteInt16 methods.

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

    // Set the 10 elements of the C-style unmanagedArray
    for (int i = 0; i < 10; i++)
    {
        Marshal.WriteInt16(unmanagedArray, i * elementSize, ((Int16)(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.ReadInt16(unmanagedArray, i * elementSize));
    }

    Marshal.FreeHGlobal(unmanagedArray);

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

Remarks

WriteInt16 enables direct interaction with an unmanaged 16-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

WriteInt16(IntPtr, Int32, Int16)

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

Writes a 16-bit signed integer value into unmanaged memory at a specified offset.

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

Parameters

ptr
IntPtr

The base address in unmanaged memory to write to.

ofs
Int32

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

val
Int16

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 ReadInt16 and WriteInt16 methods.

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

    // Set the 10 elements of the C-style unmanagedArray
    for (int i = 0; i < 10; i++)
    {
        Marshal.WriteInt16(unmanagedArray, i * elementSize, ((Int16)(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.ReadInt16(unmanagedArray, i * elementSize));
    }

    Marshal.FreeHGlobal(unmanagedArray);

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

Remarks

WriteInt16 enables direct interaction with an unmanaged 16-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

WriteInt16(Object, Int32, Char)

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

Caution

WriteInt16(Object, Int32, Char) may be unavailable in future releases.

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

C#
[System.Obsolete("WriteInt16(Object, Int32, Char) may be unavailable in future releases.")]
[System.Security.SecurityCritical]
public static void WriteInt16 (object ptr, int ofs, char val);
C#
[System.Obsolete("WriteInt16(Object, Int32, Char) may be unavailable in future releases.")]
public static void WriteInt16 (object ptr, int ofs, char val);
C#
public static void WriteInt16 (object ptr, int ofs, char val);
C#
[System.Security.SecurityCritical]
public static void WriteInt16 (object ptr, int ofs, char 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
Char

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

WriteInt16 enables direct interaction with an unmanaged 16-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)

WriteInt16(Object, Int32, Int16)

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

Caution

WriteInt16(Object, Int32, Int16) may be unavailable in future releases.

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

C#
[System.Obsolete("WriteInt16(Object, Int32, Int16) may be unavailable in future releases.")]
[System.Security.SecurityCritical]
public static void WriteInt16 (object ptr, int ofs, short val);
C#
[System.Obsolete("WriteInt16(Object, Int32, Int16) may be unavailable in future releases.")]
public static void WriteInt16 (object ptr, int ofs, short val);
C#
public static void WriteInt16 (object ptr, int ofs, short val);
C#
[System.Security.SecurityCritical]
public static void WriteInt16 (object ptr, int ofs, short 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
Int16

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

WriteInt16 enables direct interaction with an unmanaged 16-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)