SwiftMango
SwiftMango

Reputation: 15284

What is Add-Type vs square bracket?

To use a .net type, I normally do Add-Type then use New-Object:

Add-Type -AssemblyName "System.Windows.Forms"
$win = New-Object Windows.Forms.Form

But really I can also just use square bracket syntax to refer the type and use static methods:

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

What's the difference between them? I have not found much documentation with square bracket syntax for .Net types. All documentation I found for square brackets are for the arrays.

Upvotes: 6

Views: 1235

Answers (1)

mklement0
mklement0

Reputation: 438083

Add-Type is only used to load .NET types into a session (or to define them by ad hoc compilation).

To use these types by calling their constructors, you have two options:

  • In any PowerShell version:

  • Preferably, in PowerShell v5+:

    • Use a type literal ([...]) and call the type's (PowerShell-supplied) static ::new() method.
    • See this answer for more information about PowerShell type literals.

As Lee_Dailey and Theo note, using the static ::new() method has several advantages:

  • Calling ::new() is faster; with a single or only a few constructor calls that probably won't matter, but it may in loops.

  • Executing just ::new without parentheses shows all constructor overloads, which is a convenient way to find out what constructor arguments are needed and what their types are.

  • Calling ::new() doesn't wrap the newly created instance in a - mostly - invisible [psobject] wrapper, the way that New-Object and cmdlets in general do; while this wrapper is mostly benign, it can cause subtle differences in behavior - see GitHub issue #5579.

Note that, due to PowerShell's two fundamental parsing modes, New-Object and ::new() require different syntax, as discussed in this answer.

Upvotes: 7

Related Questions