I know that PowerShell function calls don't need specific parentheses like C#, but when I try to call .NET class functions, it seems parentheses are always needed, like:
[Int32]::Parse("12")
So is this a syntax rule?
I know that PowerShell function calls don't need specific parentheses like C#, but when I try to call .NET class functions, it seems parentheses are always needed, like:
[Int32]::Parse("12")
So is this a syntax rule?
The way you refer to a .NET type in general is to put the typename in square brackets e.g. '' -is [string]
. Although '' -is 'string'
works as well, I find it easier to tell that a .NET type is being referred to when square brackets are used.
The way you refer to static type members is to use ::
on the type specifier as in [System.Int32]::Parse("12")
. Note that PowerShell will prepend System.
your typename, if it can't find it as you've specified. This makes for a nice shortcut. There are also type accelerators that appear the same way e.g. [int]::Parse("12")
.
Yes, it is a rule. More specifically, when you call a method on an object (as opposed to a cmdlet or standalone function), you always use what you are calling braces ()
and you separate your arguments with commas ,
:
# A cmdlet: no use of (), parameter names and values separated by spaces
$obj = New-Object -TypeName PSObject -ArgumentList @{Name='hello'}
# A method: () are used even with no arguments
$obj.GetType()
Note that in English these are much more commonly referred to as parentheses, and braces typically refer to curly braces which are these: {}
.