Using unused affect performance?

While developing, I saw that in a large part of my classes contained a certain amount of using that were not being used and I came to doubt the title.

Does using unused affect application performance in any way? Do you always have to remove unused ones or will it make no difference?
Author: Maniero, 2014-05-21

5 answers

Understanding using and namespace

First let's make it clear that there are two types of using in C#. There is the statement which is used for feature termination. See a little more in this answer . And in that too.

But you're talking about the compiler directive. You're talking about something that works as an alias, a way to simplify access to types available to your application.

Is 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 fetch them from assemblies that are included in the project. The assembly does not need to be in the project but the compiler cannot search for assemblies that have no reference and are available in some place accessible to it.

Namespaces don't actually exist internally in the . NET. what we call namespaces are actually surnames for types. See more in this answer . Just exchange the import for using since there the answer is about VB.NET. using is used only to facilitate readability and eventually code typing (but this really helps little and is not important).

We use using to indicate to the compiler that types of that code that cannot be found in this scope should be searched in other project scopes (other sources contained in it or assemblies 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 used only to avoid name conflicts, i.e. it is an integral part of the type name, no matter how you wrote the code, by giving a full identifier, or letting the compiler find its full name for you, the final generated code , the IL, will be exactly the same.

Removing an unused using, that is, that there are no types contained in it (with that last name) being used in the code can improve the performance of the compiler and other analysis tools being used in the project or solution, since fewer unnecessary searches are performed.

For this there are tools for this like the one in Visual Studio, already mentioned, and the Resharper which is a plugin considered mandatory by many C#programmers. A tool like Resharper does not benefit much in performance from removing unused usings because it always tries to look at 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 wait big gains.

Extra information

using can define an identifier as an alias to simplify writing and avoid ambiguity, and can even 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 being able to "import" static types. That is, you will be able to import a static class, which actually always worked as if it were a namespace for methods that do not need an instance. Example:

using static System.Console;
WriteLine("Exemplo");

Is the same as writing:

System.Console.WriteLine("Exemplo");

I put on GitHub for future reference .

 30
Author: Maniero, 2020-09-03 13:15:22

Usings are only shortcut definitions for type naming (class/struct/enum / delegate)... so that it is possible to use the type names directly under the namespaces indicated by the usings:

using System;

// agora será possível usar a classe String sem especificar o namespace
class Xpto
{
    public String Nome { get; set; }
}

When removing a using, what is being done is that the shortcut can no longer be used, but it is still possible to use the full name to refer to the type:

// sem o using temos que usar o namespace junto do nome
// mas ainda assim a classe pode ser referenciada
class Xpto
{
    public System.String Nome { get; set; }
}

Both of the above forms are identical in the compiled end result.

 13
Author: Miguel Angelo, 2014-05-21 15:01:23

Do not affect the performance of the program, but may nevertheless affect the performance of the code analysis tools.

In addition, leaving usings that are not being used increases the difficulty of reading the code (what namespaces are being used in particular?).

Can also affect compile time as the compiler needs to verify that the referenced namespaces are not actually being used. use.

Edit: A good way to remove them with VS

 11
Author: Omni, 2014-05-21 14:59:21

Has no effect on execution speed, but there may be some effect on compile speed, as there are more namespaces to look for the proper class. I wouldn't worry too much about this, however you can use the Organize uses menu item to remove and sort the Using instructions.

insert the description of the image here

 9
Author: stderr, 2014-05-21 15:57:32

This does not affect the performance of the program, it is the same idea of redundant comments in the codes.

Makes no difference to the compiler but can get in the way of the programmer.

The Most that can happen is you decrease the build time of your program.

 5
Author: PauloHDSousa, 2014-05-21 14:59:37