A script #region is a script language shortcut you can use to eliminate the need to provide a traditional .NET application entry point.
Note: a script #region is not applicable to JScript.NET because it allows for global code. In other words, JScript.NET provides a mechanism whereby an author is not required to provide an explicit entry point.
A script #region is specified as follows:-
[VB.NET]
#region "Script"
' your code here
' arguments are available through the variable 'args'
#end region
[C# & Java (J#)]
#region Script
// your code here
// arguments are available through the variable "args"
#endregion
Code within the region should be considered analogous to code appearing within the Main/main entry point method. As such, you may not declare classes, or include any code not suitable for inclusion within a method. Such constructs (e.g. classes) should be specified after the end region tag.
NOTE: Java (J#) specifies a "throws java.lang.Exception, System.Exception" as part of the J# script #region declaration
If required, any usingimport or imports namespace statement should be specified before the initial region tag.
Command line arguments can be accessed within a script #region through the variable "args", which is a string array (System.String[]) automatically initialized with any arguments supplied on the command line. As such, you can for example, use the Length property (args.Length) to determine the number of actual arguments supplied.
The print method can be used within the script #region as a convenient alternative to using the System.Console.WriteLine or System.out.println methods.
The following table summarizes the requirements, restrictions and features applicable when using a script #region:-
the region must be named 'Script' |
there may only be one region specified within a multi-file script |
a script #region may only be provided in the primary script |
cannot declare classes within a region |
command line arguments available through variable 'args' |
print method available |
Java (J#) specifies "throws java.lang.Exception, System.Exception" as part of any Java script #region declaration |
not applicable to JScript.NET |
Examples
The following examples contrast a script #region with the use of a traditional entry point. See also the hellocommandLine scripts in the Tutorial folder.
[VB.NET]
#region "Script"
print("Hello VB.NET")
#end region
vs
class Script
Public shared Sub Main(args() As String)
End Sub
End class
or
module Script
Public Sub Main(args() As String)
System.Console.WriteLine("Hello VB.NET")
End Sub
End module
[C#]
#region Script
print("Hello C#");
#end region
vs
class Script
{
public static void Main(string[] args)
{
System.Console.WriteLine("Hello C#");
}
}
[Java (J#)]
#region Script
vs
class Script
{
public static void main(String[] args)
{
System.println.out("Hello J#");
}
}
[JScript.NET]
Not Applicable