The USING statement allows the types in a namespace to be used without having to specify the fully-qualified type name.
using namespaceName
using alias := namespaceName
using static typeName
namespaceName | Specifies a fully-qualified namespace name that the compiler will use when attempting to resolve type names. |
alias | An alias for the namespace. |
typeName | Specifies the name of the type whose static members and nested types can be referenced without specifying a type na |
All types in .NET have fully-qualified names consisting of a namespace name and a type name. For example, "System.Windows.Forms.MessageBox" refers to a type named "MessageBox" (a class, in this case) in the "System.Windows.Forms" namespace.
The use of namespaces in .NET helps prevent naming conflicts between class libraries. However, it can be cumbersome to type the fully qualified name. The using statement instructs the compiler to treat the specified namespace as if it were part of the current namespace, for purposes of type resolution. This allows you to specify partial type names in your source code, rather than fully-qualified names. You can, of course, always use a fully-qualified name regardless of any using statements present.
Note that the using statement only imports the type names in the specified namespace; it does not import names in nested or parent namespaces.
Also note that every source file has an implied using System and using XSharp directive, since the System and XSharp namespace contain classes that virtually every application will use. Explicitly specifying using System or using XSharp is allowed, but unnecessary.
USING System.Windows.Forms
FUNCTION Start() AS VOID
LOCAL dlg1 AS OpenFileDialog // error, without the using statement
LOCAL dlg2 AS System.Windows.Forms.OpenFileDialog // always ok, but cumbersome to type
dlg1 := OpenFileDialog{} // error, without the using statement
dlg2 := System.Windows.Forms.OpenFileDialog{} // always ok, but cumbersome to type
...
RETURN
or
USING SWF := System.Windows.Forms
FUNCTION Start() AS VOID
LOCAL dlg1 AS SWF.OpenFileDialog // use the alias in stead of the full name
dlg1 := SWF.OpenFileDialog{}
...
RETURN
or
USING STATIC System.Console
FUNCTION Start as VOID
WriteLine("Hello world") // this calls System.Console.WriteLine()
ReadLine() // this calls System.Console.ReadLine()
RETURN