VB -Parentheses! Please explain?

后端 未结 2 1541
余生分开走
余生分开走 2021-01-21 05:04
Sub Main() 

    Console.WriteLine(\"check\")   
    Console.Read()

End Sub

Why does Sub Main () need them? How do they apply to this procedure? .Wri

2条回答
  •  滥情空心
    2021-01-21 05:27

    Parenthesis are required when they are required, and optional when they are optional. In the case of empty parameter/argument lists parenthesis are "just for show".

    A Sub Procedure can be declared as Sub Main() or Sub Main - the parenthesis are optional when there are no parameters. Likewise, procedures/functions can be invoked without parenthesis if (and only if) no arguments are supplied.

    Sub A               ' ok, no parameter list - no need for parenthesis
    Sub A()             ' it's fine to use parenthesis anyway
    Sub B(x as Integer) ' need parenthesis for parameter list
    
    obj.A               ' ok, no arguments - no need for parenthesis
    obj.A()             ' it's fine to use parenthesis anyway
    obj.B(42)           ' need parenthesis when arguments are specified
    

    In the above, the definitions of A and invocations of A are equivalent as the parenthesis are optional in these cases.

提交回复
热议问题