locked
How to initialize a Generic.List PowerShell RRS feed

  • Question

  • This is a very simple question but I spent 2 hours Googling not a single one works. 

    $MyInList = New-Object System.Collections.Generic.List[string] $MyInlist.Add("sdf", "sdfsdf"); $MyInlist.Addrange(("sdf", "sdfsdf")); $MyInlist{"sdf", "sdfsdf"} $MyInlist.Add["sdf", "sdfsdf"]

    $MyInlist

    for($a=0; $a -le $MyInlist.Count; $a++){

       Write-Output $MyInlist[$a]
    }


    I have tried all possible combination suggested by Google and mine own but none work. To display the content I have $MyInlist all by itself and the for loop they display nothing.

    I dont know what I am doing wrong but both the initialize

    and to display the contents nothing works. Please suggest a fix.

    Tuesday, January 30, 2018 2:49 AM

Answers

  • Simple:

    $MyInList = New-Object System.Collections.Generic.List[string]
    PS D:\scripts> $MyInList.Add('one')
    PS D:\scripts> $MyInList.Add('two')
    PS D:\scripts> $MyInList.Add('three')
    PS D:\scripts> $MyInList
    one
    two
    three
    PS D:\scripts>

    Also:

    PS D:\scripts> $MyInList = New-Object System.Collections.Generic.List[string]
    PS D:\scripts> [string[]]$a = 'one','two','three'
    PS D:\scripts> $MyInList.AddRange($a)
    PS D:\scripts> $MyInList
    one
    two
    three
    PS D:\scripts>

    And this:

    PS D:\scripts> foreach($s in $MyInList){$s}
    one
    two
    three
    PS D:\scripts>

    And ..

    PS D:\scripts> for($i=0;$i -lt $MyInList.Count;$i++){$MyInList[$i]}
    one
    two
    three
    PS D:\scripts>


    \_(ツ)_/

    Tuesday, January 30, 2018 3:12 AM
  • The code is not PowerShell.  It is the Net Framework which is exposed to PowerShell.  Net namespaces that aren't already loaded can be easily loaded on demand and call to net can just be types in or scripted.

    The namespace for "generics" is System.Collections.Generic. This is one of the pre-loaded naespaces.

    Other namespaces can be loaded such as System.Windows.Forms.

    Add-Type -AssemblyName  System.Windows.Forms

    The types within a namespce are referenced with [] in PowerSHell.

    [System.Windows.Forms.Form]

    Static methods and properties within a namespace are access with ::

    $f = [System.Windows.Forms.Form]::new()
    $f.ShowDialog()

     $alist = [System.Collections.Arraylist]::New()
     $alist.Add(1)


    \_(ツ)_/

    Tuesday, January 30, 2018 4:56 AM

All replies

  • Simple:

    $MyInList = New-Object System.Collections.Generic.List[string]
    PS D:\scripts> $MyInList.Add('one')
    PS D:\scripts> $MyInList.Add('two')
    PS D:\scripts> $MyInList.Add('three')
    PS D:\scripts> $MyInList
    one
    two
    three
    PS D:\scripts>

    Also:

    PS D:\scripts> $MyInList = New-Object System.Collections.Generic.List[string]
    PS D:\scripts> [string[]]$a = 'one','two','three'
    PS D:\scripts> $MyInList.AddRange($a)
    PS D:\scripts> $MyInList
    one
    two
    three
    PS D:\scripts>

    And this:

    PS D:\scripts> foreach($s in $MyInList){$s}
    one
    two
    three
    PS D:\scripts>

    And ..

    PS D:\scripts> for($i=0;$i -lt $MyInList.Count;$i++){$MyInList[$i]}
    one
    two
    three
    PS D:\scripts>


    \_(ツ)_/

    Tuesday, January 30, 2018 3:12 AM
  • Thank you JRV thank you very much. I had just one question, I am from the C generation and those days there was always MSDN documentation and that answered all questions. Today if I want such a documentation on Power Shell and all it syntax and usage what is search string I should type? Please do not give me the URL but the search string which will take me to that URL. Thanks again

    best regards

    ananda

    Tuesday, January 30, 2018 4:43 AM
  • The code is not PowerShell.  It is the Net Framework which is exposed to PowerShell.  Net namespaces that aren't already loaded can be easily loaded on demand and call to net can just be types in or scripted.

    The namespace for "generics" is System.Collections.Generic. This is one of the pre-loaded naespaces.

    Other namespaces can be loaded such as System.Windows.Forms.

    Add-Type -AssemblyName  System.Windows.Forms

    The types within a namespce are referenced with [] in PowerSHell.

    [System.Windows.Forms.Form]

    Static methods and properties within a namespace are access with ::

    $f = [System.Windows.Forms.Form]::new()
    $f.ShowDialog()

     $alist = [System.Collections.Arraylist]::New()
     $alist.Add(1)


    \_(ツ)_/

    Tuesday, January 30, 2018 4:56 AM
  • [System.Collections.Generic.List[string]]::new()

    The static function NEW() doesn't seem to work in PS 4, but works fine in PS5.  Just fyi.

    Wednesday, December 4, 2019 10:47 PM