Skip to content

Instantly share code, notes, and snippets.

@jlollis
Forked from kevinblumenfeld/Types.md
Created September 27, 2020 07:52
Show Gist options
  • Save jlollis/b880cdbe29d232d82d34e803180d3da0 to your computer and use it in GitHub Desktop.
Save jlollis/b880cdbe29d232d82d34e803180d3da0 to your computer and use it in GitHub Desktop.

Revisions

  1. @kevinblumenfeld kevinblumenfeld revised this gist Mar 9, 2018. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion Types.md
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    Credit: Mark Krauss
    Credit: Mark Kraus
    Website: https://get-powershellblog.blogspot.com
    # Collection Type Guidence
    ## When to use what
  2. @kevinblumenfeld kevinblumenfeld revised this gist Mar 9, 2018. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions Types.md
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,5 @@
    Credit: Mark Krauss
    Website: https://get-powershellblog.blogspot.com
    Credit: Mark Krauss
    Website: https://get-powershellblog.blogspot.com
    # Collection Type Guidence
    ## When to use what
    * Use Arrays if you know the element types and have a fixed length and/or known-up-front collection size that will not change.
  3. @kevinblumenfeld kevinblumenfeld revised this gist Mar 9, 2018. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions Types.md
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,5 @@
    Credit: Mark Krauss
    Website: https://get-powershellblog.blogspot.com
    # Collection Type Guidence
    ## When to use what
    * Use Arrays if you know the element types and have a fixed length and/or known-up-front collection size that will not change.
  4. @kevinblumenfeld kevinblumenfeld created this gist Mar 9, 2018.
    91 changes: 91 additions & 0 deletions Types.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,91 @@
    # Collection Type Guidence
    ## When to use what
    * Use Arrays if you know the element types and have a fixed length and/or known-up-front collection size that will not change.
    * Use ArrayList if you have an unkown collection size with either unknown or mixed type elements.
    * Use a Generic List<t> when know the type of the elements but not the size of the collection.
    * Use a HashTable if you are going to do key based lookups on a collection and don't know the object type of the elements.
    * Use a Dictionary<TKey, TValue> you are going to do key based lookups on a collection and you know the type of the elements.
    * Use a HashSet<t> when you know the type of elements and just want unique values and quick lookups and assignmnets.
    * Use LinkList<t> if you are going to make large numbers of additions and subtractions to an ordered list (and have the understanding to use this type)
    * Use Queue<t> if you will build a collection that will need to be worked on First-in-first-out FIFO
    * Use Stack<t> if you will build a collection that will need to be worked Last-in-first-out LIFO
    * Use SortedSet<T> when you need a HasSet<t> like set, but sorted (alaphbetically, for example)
    * Use SortedList<t> when you need a List<t>, but sorted (alaphbetically, for example)
    * Use SortedDictionary<TKey, TValue> when you need a Dictionary<TKey, TValue>, but sorted (alaphbetically, for example)

    ## Avoid the following:
    * Do not use Object[]
    * Do not use += on Arrays. If your collection will grow or shrink, use ArrayList or List<t>
    * Do not use List<Object> for value types (int32, int64, char, etc)
    * avoid using Arrays, Lists, and ArrayLists for lookup/search operations, use ditcionaries and sets instead
    * Linked lists should only be considered in rediculously high volume add/remove operations to a list as the code complexity is too much for PowerShell
    * Only use "sorted" types when you really need to. If you only need it for reconstituion, sorting the keys and then foreaching them can work on small collections

    ## Examples
    ### Arrays
    https://docs.microsoft.com/en-us/dotnet/api/system.array?view=netframework-4.7.1
    string array:
    ```powershell
    $Array = [string[]]@('string1','string2')
    ```
    Int array:
    ```powershell
    $Array = [int[]]@(1,2,3,4,5)
    ```
    ### ArrayList
    https://docs.microsoft.com/en-us/dotnet/api/system.collections.arraylist?view=netframework-4.7.1
    ```powershell
    $ArrayList = [System.Collections.ArrayList]::new()
    $null = $ArrayList.Add(1)
    $null = $ArrayList.Add('String1')
    ```
    ### List<t>
    https://docs.microsoft.com/en-us/dotnet/api/system.collections.generic.list-1?view=netframework-4.7.1
    String:
    ```powershell
    $List = [System.Collections.Generic.List[String]]::new()
    $List.Add('String1')
    $List.Add('String2')
    ```
    Int:
    ```powershell
    $List = [System.Collections.Generic.List[Int]]::new()
    $List.Add(1)
    $List.Add(2)
    ```
    ### Hashtable
    https://docs.microsoft.com/en-us/dotnet/api/system.collections.hashtable?view=netframework-4.7.1
    ```powershell
    $HashTable = @{
    Key1 = "Value1"
    Key2 = "Value2"
    }
    ```
    ### Dictionary
    https://docs.microsoft.com/en-us/dotnet/api/system.collections.generic.dictionary-2?view=netframework-4.7.1
    Case Sensitive (default)
    ```powershell
    $Dictionary = [System.Collections.Generic.Dictionary[String,String]]::New()
    $Dictionary['key1'] = 'Value1'
    $Dictionary['key2'] = 'Value2'
    # Is case sensitiveby default:
    $Dictionary['Key2'] = 'Value3'
    ```
    Case Insensitive:
    ```powershell
    $Comparer = [System.StringComparer]::InvariantCultureIgnoreCase
    $Dictionary = [System.Collections.Generic.Dictionary[String,String]]::New($Comparer)
    $Dictionary['key1'] = 'Value1'
    $Dictionary['key2'] = 'Value2'
    # Will repllace the key above
    $Dictionary['Key2'] = 'Value3'
    ```
    Example using processes and looking them up by PID:
    ```powershell
    $ProcessDict = [System.Collections.Generic.Dictionary[int,System.Diagnostics.Process]]::new()
    Get-Process | ForEach-Object {
    $ProcessDict[$_.Id] = $_
    }
    $ProcessDict[0] | format-list *
    ```