
These implicit global using directives include the most common namespaces for the project type. NET 6 SDK also adds a set of implicit global using directives for projects that use the following SDKs: For more information see the article on New C# templates generate top level statements Your application may not match the code in this article, if you've already upgraded to the. You can also globally include a namespace by adding a item to your project file, for example. The following example enables using all the methods declared in the System.Math in all files in your project: global using static System.Math In both cases, the directive's scope is all files in the current compilation. The global modifier may be applied to a using alias directive. The global modifier may be combined with the static modifier. The order of global using directives doesn't matter, either in a single file, or between files.

Typically, you'll want to keep them in a single location. You may add global using directives to any source file. All namespace and type declarations in the file.All using directives without the global modifier.All global using directives in a single file must appear before: Where fully-qualified-namespace is the fully qualified name of the namespace whose types can be referenced without specifying the namespace.Ī global using directive can appear at the beginning of any source code file. The global using directive was added in C# 10. global modifierĪdding the global modifier to a using directive means that using is applied to all files in the compilation (typically a project). For a list of the system-defined namespaces, see. User-defined namespaces are namespaces defined in your code. Namespaces come in two categories: user-defined and system-defined. A using directive doesn't give you access to any namespaces that are nested in the namespace you specify. Otherwise, compiler error CS1529 is generated.Ĭreate a using directive to use the types in a namespace without having to specify the namespace. In any namespace, but before any namespaces or types declared in that namespace, unless the global modifier is used, in which case the directive must appear before all namespace and type declarations.At the beginning of a source code file, before any namespace or type declarations.The scope of a using directive without the global modifier is the file in which it appears. For more information about the using statement, see using Statement. The using keyword is also used to create using statements, which help ensure that IDisposable objects such as files and fonts are handled correctly.
