Importing Namespace
Explore how to simplify C# code by importing namespaces using traditional and global using directives. Understand how namespaces like System are implicitly imported and how to manage them in .NET 6 and later projects. Gain practical knowledge on controlling and customizing namespace imports to improve project structure and IntelliSense support.
System is a namespace, like an address for a type. To refer to someone’s location strictly, we might use Oxford.HighStreet.BobSmith, which tells us to look for a person named Bob Smith on the High Street in Oxford. System.Console.WriteLine tells the compiler to look for a method called WriteLine in a type named Console in a namespace named System.
To simplify our code, the Console App project template for every version of .NET before 6.0 added a statement at the top of the code file to tell the compiler to always look in the System namespace for types that haven’t been prefixed with their namespace, as shown in the following code:
We call this importing the namespace. The effect of importing a namespace is that all available types in that namespace will be available to our program without needing to enter the namespace prefix and will be seen in IntelliSense. At the same time, we write code. .NET Interactive Notebooks have most namespaces imported automatically.