Ask Learn
Preview
Please sign in to use this experience.
Sign inThis browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Use the namespace alias qualifier ::
to access a member of an aliased namespace. You can use the ::
qualifier only between two identifiers. The left-hand identifier can be one of a namespace alias, an extern alias, or the global
alias. For example:
A namespace alias created with a using alias directive:
using forwinforms = System.Drawing;
using forwpf = System.Windows;
public class Converters
{
public static forwpf::Point Convert(forwinforms::Point point) => new forwpf::Point(point.X, point.Y);
}
An extern alias.
The global
alias, which is the global namespace alias. The global namespace is the namespace that contains namespaces and types that aren't declared inside a named namespace. When used with the ::
qualifier, the global
alias always references the global namespace, even if there's the user-defined global
namespace alias.
The following example uses the global
alias to access the .NET System namespace, which is a member of the global namespace. Without the global
alias, the user-defined System
namespace, which is a member of the MyCompany.MyProduct
namespace, would be accessed:
namespace MyCompany.MyProduct.System
{
class Program
{
static void Main() => global::System.Console.WriteLine("Using global alias");
}
class Console
{
string Suggestion => "Consider renaming this class";
}
}
Note
The global
keyword is the global namespace alias only when it's the left-hand identifier of the ::
qualifier.
You can also use the .
token to access a member of an aliased namespace. However, the .
token is also used to access a type member. The ::
qualifier ensures that its left-hand identifier always references a namespace alias, even if there exists a type or namespace with the same name.
For more information, see the Namespace alias qualifiers section of the C# language specification.
.NET feedback
.NET is an open source project. Select a link to provide feedback:
Please sign in to use this experience.
Sign in