Home > Software engineering >  Making .NET array type in Powershell
Making .NET array type in Powershell

Time:03-26

i'm trying to make System.Memory[char] .

[System.Memory[char]]::Memory([char],0,10) * says it can't find System.Memory type .

Also tried *

[System.Memory`3 [[char],0,10]]@()

Solution: The issue seems to be the .NET version used by the Powershell .

CodePudding user response:

It is the static pseudo method ::new(), introduced in PowerShell v5 that provides access to a type's constructors.

# Initialize a [System.Memory[char]] instance with 10 NUL (0x0) chars.
[System.Memory[char]]::new((, [char] 0) * 10)

Note: The two System.Memory`1 constructors both require a [char[]] array as an argument. The two additional arguments in the 3-parameter overload, start and length, must refer to a range of elements within that array.

The above simply creates a 10-element array to begin with (using NUL characters), obviating the need for the additional arguments.


Note:

  • [System.Memory[char]]@() does not work, because in order for PowerShell to translate this cast to a single-parameter constructor call, the operand must be a [char[]] array:

    • [System.Memory[char]] [char[]] @()
  • Fundamentally, the System.Memory`1 type is available only in .NET Core 2.1 / .NET 5 .

  • Related