Search code examples
.netfunctionpowershellcallbraces

Powershell: Does calling .NET a function always require parenthesis?


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?


Solution

  • 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: {}.