As a user I want the "struct" keyword so that I can create custom objects derived from System.ValueType.
I found a workaround using C# code with Add-Type.
Here is some PowerShell code that shows a limitation of using a class vs struct. The first highlight shows the PowerShell custom class array is not initialized. The second highlight shows how to get the desired behavior using a C# struct.

This falls under #6418 I think, and is one of many requests around class improvements.
As a user I want the "struct" keyword so that I can create custom objects derived from System.ValueType.
Can you expand a little bit more about what you're trying to solve?
I ask because a significant amount of the benefits that structs typically provide are lost in PowerShell due to boxing at just about every step behind the scenes.
This falls under #6418 I think, and is one of many requests around class improvements.
I looked at that previously and did not see any mention of "struct"
Huh, you're right, I must have grabbed the wrong issue number. Was it #6652? Hmm... close, but structs aren't mentioned there. I know I've seen 'em mentioned before, but not sure where it is at the moment.
As a user I want the "struct" keyword so that I can create custom objects derived from System.ValueType.
Can you expand a little bit more about what you're trying to solve?
I ask because a significant amount of the benefits that structs typically provide are lost in PowerShell due to boxing at just about every step behind the scenes.
I would like to be able to create an array of custom objects in a single call like I can do with built-in types.
For example...
$arr = New-Object int[] 1000
$arr[400] = 1234
If I had the "struct" keyword, I would be able to do this...
struct MyPoint {[int]$x; [int]$y}
$arr = New-Object MyPoint[] 1000
$arr[400].x = 1234
$arr[400].y = 4321
...instead of...
class MyPoint {[int]$x; [int]$y}
$arr = New-Object MyPoint[] 1000
0..999 | % {$arr[$_] = New-Object MyPoint}
$arr[400].x = 1234
$arr[400].y = 4321
Hi @lenihan - it would help us to understand your request if you can explain what you want to do with structs that you can't already do with classes. What scenario are you trying to achieve that requires this new feature? What scenarios do you have that are blocked without it? Thanks.
Yeah, I'm not sure what here requires the use of structs. You can do this without even using classes if you just need custom objects.
# with classes
class MyPoint { $x = 0; $y = 0 }
$array = foreach ($i in 1..1000) { [MyPoint]::new() }
# without classes
$array = foreach ($i in 1..1000) { [PSCustomObject]@{ x = 0; y = 0 } }
@bpayette @vexx32
Here is what I want to do...
$arr = New-Object Point[] 10000000Write-Host $arr[124232].X or $arr[9242422].Y = 1232.232Thoughts?
Thoughts?
Honestly that's one of the few situations where I will say PowerShell just isn't really going to work. The overhead of an optimized variable lookup alone could choke at 10 million data points, let alone the dynamic indexing, boxing, etc.
Even C# might not be great for that. Unless you're intimately familiar with when a struct would be copied, when to use ref or Span<T> or realistically unmanaged pointers, that's still going to be very difficult write with acceptable performance.
When you're looking at that kind of scale you even need to start thinking about things like skipping the implicit bounds check of a normal array index operation to squeeze every inch of performance you can. When you're analyzing performance at that level, PowerShell just isn't part of the conversation.
@SeeminglyScience
I already have C# code that can load large datasets quickly using unmanaged pointers.
Is it possible to present that data in powershell as an array that can be accessed/modified (ie $arr[221324] = 1.234).
You can implement a wrapper class (in C#) with an indexer that accesses the unmanaged pointer
I can only think PowerShell call C# API with struct parameter. This looks like Span