According to the offical document about arrays in PowerShell, several methods are defined for arrays, for example Clear(), ForEach(), Where(), etc. Following code tested these methods:
$arr = 1..2
$arr.Clear()
$arr.Length
write "--------------------------------------------------"
$arr = 1..2
$arr.ForEach({$_ + 1})
write "--------------------------------------------------"
$arr = 65..90
$arr.Where({($_ % 2) -eq 0})
Output:
2
--------------------------------------------------
2
3
--------------------------------------------------
66
68
70
72
74
76
78
80
82
84
86
88
90
Fine! And, methods such as ForEach() have many overloads that are not tested here.
But where these methods are defined? I mean, What is the class that contains the definition of these methods? As far as I know, these methods are not defined in .net core. (I use PowerShell 7)
Clear()
is part of theIList
interface implemented bySystem.Array
, which is the base type for collections (System.Object[]
) in PowerShell.To see all native methods available, use:
However: The
Where()
andForEach()
"magic" methods were introduced in v4 and are actually PowerShell-specific extension methods (as more performant alternatives toForEach-Object
andWhere-Object
), defined inSystem.Management.Automation.EnumerableOps
. Have a look at the source: