Understanding the using and the namespace
First let’s make it clear that there are two types of using
in C#. There is the statement which is used for resource completion. See a little more in that reply. And in that also.
But you’re talking about the compiler directive. You’re talking about something that works as an alias, a way to simplify access to available types for your application.
It’s good to note that you can only put in using
the namespaces and types that are referenced in your project. That is, the compiler will only try to get them in assemblys which are included in the project. The Assembly does not need to be in the project but the compiler cannot search in assemblys which has no reference and which is available at any place accessible to it.
Namespaces actually do not exist internally in .NET. What we call namespaces actually are surnames for types. See more in that reply. Just change the import
for using
since there the answer is about VB.NET. The using
is only used to facilitate readability and eventually the typing of the code (but this really helps little and is not important).
We use the using
to indicate to the compiler that the types of that code cannot be found in this scope shall be searched for in other scopes of the project (other sources contained therein or assemblys referenced in the project, but the search (lookup) should be done only in certain families, in certain surnames, making the search faster and avoiding possible ambiguities of types.
Performance
If a namespace is only used to avoid name conflicts, i.e., it is an integral part of the type name, no matter how you wrote the code, giving a complete identifier, or letting the compiler find its full name by you, the final code generated, the IL, will be exactly the same.
Withdraw a using
unused, i.e., that there are no types contained in it (with that surname) being used in the code can improve compiler performance and other analysis tools being used in the project or solution, as fewer unnecessary searches are performed.
That is why there are tools for this like the one in Visual Studio, already mentioned, and Resharper which is a plugin considered mandatory by many C#programmers. A tool like Resharper does not benefit much in performance from the withdrawal of using
s not used because it always tries to look for the solution as a whole to give better information and propose better actions in the code, regardless of the use of using
(depends on configuration).
But don’t expect big gains.
Extra information
The using
can define an identifier as an alias to simplify writing and avoid ambiguity, including can determine generic types. It is a form of typedef for those coming from C/C++. Example:
using ImageControl = System.Windows.Controls.Image;
using Image = System.Drawing.Image;
using ListString = System.Collections.Generic.List<string>;
ListString lista; // a variável lista será do tipo System.Collections.Generic.List<string>;
ImageControl img1; // img1 será do tipo System.Windows.Controls.Image
Image img2; // img1 será do tipo System.Drawing.Image sem nenhum conflito com o Image acima
C# 6
In C# 6 we still have a new form of using
can "import" static types. That is, you can import a static class, which actually always worked as if it were a namespace for methods that do not require an instance. Example:
using static System.Console;
WriteLine("Exemplo");
is the same as writing:
System.Console.WriteLine("Exemplo");
I put in the Github for future reference.
C# 10
It should be possible to make a global import and you will not need to use certain namespaces in each code file.
Definitely not... affects the readability of the code however.
– Miguel Angelo
So there are tools that remove all directives
using
unused. Do not confuse with the statementusing
.– Maniero
@bigown: good observation! Documentation on the statement using and on the using directive
– Miguel Angelo