Can unused namespace directives cause any changes?

I'm sure the codes will produce the same result, but will the CLR consider unused namespace directives when allocating memory? Another question might be that the CLR considers unused blocks of code when allocating memory?

+4
source share
2 answers

At runtime it will not matter if you have unused using directives in your code or not.

The using directive is available for convenience so you can write

 using System.IO; [...] string path = Path.GetDirectoryName(filename); 

instead of writing the full name

 string path = System.IO.Path.GetDirectoryName(filename); 

every time you want to use a type from the System.IO namespace. The directive tells the compiler that namespaces should look for the types used in the file. Then the compiler will actually replace the first example with the second, that is, the IL code in the assembly will always use fully qualified type names. Unused namespaces will not be displayed in the compiled assembly.

However, there are reasons to keep a clean list of imported namespaces. John Faminella stated in a related question :

There are several reasons why you would like to take them out.

  • It's pointless. They do not add value.
  • This is confusing. What is used in this namespace?
  • If you do not, you will gradually accumulate meaningless use with code change time.
  • Static analysis is slower.
  • Code compiles faster.
+3
source

To add 0xA3 to the answer: using directives exist only in C # source code. Like comments, they do not exist in a binary assembly.

+1
source

Source: https://habr.com/ru/post/1316635/


All Articles