Using unused affect performance?

38

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

Using unused affects the performance of the application in any way? Do you always need to remove the unused ones or will it make no difference?

    
asked by anonymous 21.05.2014 / 16:53

5 answers

25

Understanding using and the namespace

First let's make it clear that there are two types of using in C #. There is the statement that is used for finalizing features. See a little more in this answer . And this 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.

It's good to note that you can only put% namespaces and types that are referenced in your project. That is, the compiler will only try to fetch them from the assemblys that are included in the project. The assembly does not need to be in the project, but the compiler can not look at assemblys that has no reference and is available in some accessible location.

21.05.2014 / 18:19
11

Usings are just shortcut definitions for type naming (class / struct / enum / delegate) ... so you can use the 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 compiled final result.

    
21.05.2014 / 17:01
10

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

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

They can also affect compilation time since the compiler needs to verify that the referenced namespaces are not actually being used.

Edit: A good way to remove them with VS

    
21.05.2014 / 16:59
8

It has no effect on execution speed, but there may be some effect on compile speed, as there are more namespaces to search for the appropriate class. I would not worry too much about this, however you can use the Organize Usings item in the menu to remove and sort the using statements.

    
21.05.2014 / 17:02
5

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

It does not make a difference to the compiler but it can get in the way of the programmer.

The maximum that can happen is that you shorten the build time of your program.

    
21.05.2014 / 16:59