4

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 .

1
  • As an aside re `<n> in PowerShell type literals, such as [System.Memory`1] (represented in C# as System.Memory<T>): <n> refers to the number of type parameters in generic .NET types. Thus, [System.Memory`1] implies one type parameter, which is correctly supplied as [char] in your first attempt: [System.Memory[char]] (which you could also express as [System.Memory`1[char]], but that is only necessary for accessing nested types; see link). <n> is unrelated to the number of constructor arguments. Commented Mar 27, 2022 at 17:07

1 Answer 1

5

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

# Initialize a [System.Memory[char]] instance with 10 NUL (0x0) chars,
# from a [char[]] array.
[System.Memory[char]]::new(
  [char[]]::new(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 (implicitly using NUL characters), obviating the need for the additional arguments.

If you wanted the input array to use a given character other than NUL, you could use something like , [char] 'x' * 10:
, [char] 'x' create a single-element array with char. 'x', which * 10 then replicates to return a 10-element array. Note that the array will be [object[]]-typed, not [char[]]-typed, but it still works.


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+.

    • The simplest way to check if the type is available in your PowerShell session is if [bool] $IsCoreClr returns $true - in other words: you need to be running PowerShell (Core) 7+, the modern, cross-platform, install-on-demand edition of PowerShell.

[1] In earlier PowerShell versions you need to use the New-Object cmdlet, which uses argument(-parsing) mode, as all cmdlets do. As such, its syntax doesn't map cleanly onto the expression-mode syntax that is familiar from method/constructor calls, especially with respect to passing a single argument that is an array, as in this case:
New-Object System.Memory[char] -ArgumentList (, (New-Object char[] 10))
Note the need to wrap the array constructed by , [char] 0) * 10 in another array, namely a transitory one that is needed to make New-Object treat the original array as a single argument for the target constructor.
Additionally, ::new() performs better, though that will often not matter. See this answer for details.

Sign up to request clarification or add additional context in comments.

12 Comments

i did try that too, says can't find System.Memory type
@irvnriir, please see my update. There is indeed no System.Memory type, but there is System.Memory`1, aka System.Memory<T>
[System.Runtime.InteropServices.RuntimeEnvironment]::GetSystemVersion() outputs v4[..] , this is the reason ? isn't Windows Powershell always running on the .NET Framework 4.[..] ?
@irvnriir, this method seems to return a value that is frozen in time for backward compatibility. Use the following instead to see the true framework version: [System.Runtime.InteropServices.RuntimeInformation]::FrameworkDescription. Yes, Windows PowerShell runs on the legacy, Windows-only .NET Framework, whose latest and last version is v4.8.x
the command doesn't work in this Powershell, tried also with write-host and write-output .
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.