2

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)

Ding Xin
  • 307
  • 1
  • 3
  • 11

2 Answers2

1

Clear() is part of the IList interface implemented by System.Array, which is the base type for collections (System.Object[]) in PowerShell.

To see all native methods available, use:

$arr.PSObject.Methods
# or
Get-Member -InputObject $arr -MemberType Methods

However: The Where() and ForEach() "magic" methods were introduced in v4 and are actually PowerShell-specific extension methods (as more performant alternatives to ForEach-Object and Where-Object), defined in System.Management.Automation.EnumerableOps. Have a look at the source:

internal static object Where(IEnumerator enumerator, ScriptBlock expressionSB, WhereOperatorSelectionMode selectionMode, int numberToReturn)

internal static object ForEach(IEnumerator enumerator, object expression, object[] arguments)
marsze
  • 15,079
  • 5
  • 45
  • 61
  • Very helpful! Thank you very much. – Ding Xin Oct 12 '20 at 18:06
  • Yes! I accepted it. Thank you again. I just posted a new question about PowerShell, can you give me some suggestion? (https://stackoverflow.com/questions/64711177/why-host-ui-rawui-readkey-method-captures-a-not-really-pressed-enter-key) – Ding Xin Nov 06 '20 at 08:31
-1

Why do you state As far as I know, these methods are not defined in .net core. (I use PowerShell 7)? Really:

$PSVersionTable.PSVersion | Out-Default
$arr = 1..2
$arr.GetType() | Get-Member -MemberType Methods -Static
Major  Minor  Patch  PreReleaseLabel BuildLabel
-----  -----  -----  --------------- ----------
7      0      3


   TypeName: System.Object[]

Name            MemberType Definition
----            ---------- ----------
AsReadOnly      Method     static System.Collections.ObjectModel.ReadOnlyCollection[T] …
BinarySearch    Method     static int BinarySearch[T](T[] array, int index, int length,…
Clear           Method     static void Clear(array array, int index, int length)
ConstrainedCopy Method     static void ConstrainedCopy(array sourceArray, int sourceInd…
ConvertAll      Method     static TOutput[] ConvertAll[TInput, TOutput](TInput[] array,…
Copy            Method     static void Copy(array sourceArray, array destinationArray, …
CreateInstance  Method     static array CreateInstance(type elementType, int length), s…
Empty           Method     static T[] Empty[T]()
Equals          Method     static bool Equals(System.Object objA, System.Object objB)
Exists          Method     static bool Exists[T](T[] array, System.Predicate[T] match)
Fill            Method     static void Fill[T](T[] array, T value), static void Fill[T]…
Find            Method     static T Find[T](T[] array, System.Predicate[T] match)
FindAll         Method     static T[] FindAll[T](T[] array, System.Predicate[T] match)
FindIndex       Method     static int FindIndex[T](T[] array, System.Predicate[T] match…
FindLast        Method     static T FindLast[T](T[] array, System.Predicate[T] match)
FindLastIndex   Method     static int FindLastIndex[T](T[] array, System.Predicate[T] m…
ForEach         Method     static void ForEach[T](T[] array, System.Action[T] action)
IndexOf         Method     static int IndexOf(array array, System.Object value), static…
LastIndexOf     Method     static int LastIndexOf(array array, System.Object value), st…
new             Method     System.Object[] new(int )
ReferenceEquals Method     static bool ReferenceEquals(System.Object objA, System.Objec…
Resize          Method     static void Resize[T]([ref] T[] array, int newSize)
Reverse         Method     static void Reverse(array array), static void Reverse(array …
Sort            Method     static void Sort(array array), static void Sort(array keys, …
TrueForAll      Method     static bool TrueForAll[T](T[] array, System.Predicate[T] mat…
JosefZ
  • 28,460
  • 5
  • 44
  • 83