Forum Discussion

ahinterl's avatar
ahinterl
Copper Contributor
Jul 02, 2025

Array and array member methods

The following PowerShell code:

class MyClass { }
class MyClass1 : MyClass {
    [void] OutMsg([string] $Str) {
        Write-Host -Object "MyClass1: $Str"
    }
}
class MyClass2 : MyClass {
    [void] OutMsg([string] $Str) {
        Write-Host -Object "MyClass2: $Str"
    }
}
[MyClass[]] $ClassArray = @([MyClass1]::new(), [MyClass2]::new())
$ClassArray.OutMsg('TestString')

outputs:

MyClass1: TestString
MyClass2: TestString

A

Get-Member -InputObject $ClassArray

shows me that the array object itself has no OutMsg method.

Why can I successfully call

$ClassArray.OutMsg('TestString')

though (looks like this calls the OutMsg method of each array member in turn)?

3 Replies

  • ahinterl's avatar
    ahinterl
    Copper Contributor

    Hi Andres-Bohren​,

    Use the Get-Member cmdlet with the -InputObject parameter to list the members of the array object. OutStr is not listed there.

    To make sure, I transferred the code to C# and tested there: Visual Studio expectedly reports that OutStr doesn't exist for the array object (ClassArray).

    So, I guess that PowerShell does some magic here. Leaves the question where else this kind of magic is applied to, and to what degree I can trust PowerShell to treat things "natively" (I for my part will check in C# if in doubt)...

  • Andres-Bohren's avatar
    Andres-Bohren
    Steel Contributor

    HI ahinterl​ 

    Let's check without an Array

    class MyClass { }
    class MyClass1 : MyClass {
         [void] OutMsg([string] $Str) {
             Write-Host -Object "MyClass1: $Str"
         }
     }
    [MyClass[]] $DemoClass = [MyClass1]::new()
    $DemoClass | gm
    
    TypeName: MyClass1
    
    Name        MemberType Definition
    ----        ---------- ----------
    Equals      Method     bool Equals(System.Object obj)
    GetHashCode Method     int GetHashCode()
    GetType     Method     type GetType()
    OutMsg      Method     void OutMsg(string Str)
    ToString    Method     string ToString()

     

    If it is an Array you have to select the Item (or i think of a Line in a Spreadsheet) with [item]

     

    class MyClass { }
    class MyClass1 : MyClass {
         [void] OutMsg([string] $Str) {
             Write-Host -Object "MyClass1: $Str"
         }
     }
    class MyClass2 : MyClass {
         [void] OutMsg([string] $Str) {
             Write-Host -Object "MyClass2: $Str"
         }
     }
    [MyClass[]] $ClassArray = @([MyClass1]::new(),[MyClass1]::new())
    $ClassArray[0] | gm
    
    TypeName: MyClass1
    
    Name        MemberType Definition
    ----        ---------- ----------
    Equals      Method     bool Equals(System.Object obj)
    GetHashCode Method     int GetHashCode()
    GetType     Method     type GetType()
    OutMsg      Method     void OutMsg(string Str)
    ToString    Method     string ToString()

    It's all there :)

    Kind Regards
    Andres

Resources