Using unused affect performance?

Asked

Viewed 2,270 times

43

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

Using unused affects the performance of the application in any way? It is always necessary to remove unused ones or it will not make a difference?

  • 2

    Definitely not... affects the readability of the code however.

  • 1

    So there are tools that remove all directives using unused. Do not confuse with the statement using.

  • @bigown: good observation! Documentation on the statement using and on the using directive

5 answers

30


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 usings 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.

  • 2

    The directive using just doesn’t compare well with typedef as there is no way to extend its scope to multiple files. C++ does this with a typedef inside a header file, but in C# this does not exist (unfortunately =).

13

Usings are only shortcut definitions for type naming (class/struct/Enum/delegate)... so it is possible to use type names directly under the namespaces indicated by 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 final compiled result.

11

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

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

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

Edit: A good way to remove them with the VS

  • Probably, the compiler will not check whether or not usings are being used, instead will build a lookup table of type names taking into account all usings, which takes time anyway.

  • @Miguelangelo from what I understand the compiler checks the dependencies and during the build removes/ignores the ones that are not being used. So much so that the unused dependencies will not be part of the generated IL.

  • He will do this independently of the usings. That is, having or not usings, what defines whether a reference to an external library will be made or not is the use of any type contained in this. In the absence of usings, the code can still refer to the types of the library, simply indicating the full name of the type.

  • You confused usings with references to libraries... what the compiler removes are references to unused libraries and not the usings themselves.

9

It has no effect on running speed, but there may be some effect on build speed as there is more namespaces to search for the proper class. I wouldn’t worry too much about it though you can use the item Organize Usings from the menu to remove and sort the instructions using.

inserir a descrição da imagem aqui

5

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

It makes no difference to the compiler but can disturb the programmer.

The most that can happen is you decrease the compile time of your program.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.