Does the namespace on each page use bad practice / decrease performance or memory usage

In C #, we write using System.IO or any other namespace that we want to use.

Is it a bad habit, does it affect performance or memory?

Or is it useful to create wrapper classes for them and use it so that you don't use the same namespace everywhere?

+4
source share
5 answers

In .Net, the namespace is always an integral part of the name of each type. However, if you needed to specify the full namespace every time you declare a certain type, this would cause a huge repetition and noise in the code. The using directive is used for this; it essentially brings all the prefixes into one place, forcing the "last" section of the type to be specified. This can only be done if there is no ambiguity.

However, the compiler does not care about this. Thus, there is a precompilation stage where each type declaration that relies on the using directive gets its prefix back.

So when you say:

 Using System; void foo() { String s1 = "bla"; String s2 = "bli"; } 

What happens during precompilation is that a System namespace is added to each String declaration, for example:

 void foo() { System.String s1 = "bla"; System.String s2 = "bli"; } 

And only now the compiler really enters.

So, about performance, technically, it can affect the performance of the build process. The more you get, the more matches you need to complete at the pre-compilation stage: therefore, the compiler sees String . What is this String ? Is it System.String or is it SomeOtherNamespace.String ? What really happens is that the compiler adds every namespace that it finds when used to the type declaration and checks to see if that type exists. If so, great, if not, he will try the next namespace.

So, you see that if you have a lot of files with unused using declarations, the compiler will definitely do the redundant work. In extreme cases, this can significantly degrade the performance of the assembly itself.

In general, never be shy about using something that you are using (no pun intended). But you should avoid declaring unnecessary using directives, not only because of the potential (unlikely) performance impact on build time, but also because you want your code to be as clean as possible.

+2
source

You can have as many using statements at the top of the file as you want, it will affect performance a bit.

Creating wrappers for classes, however, will affect performance, making the code a little slower. Not so much, so you can use wrappers very well if it makes the code more manageable, but using it just to avoid using statements is not a good reason.

Having a large number of using statements may slightly affect compilation time, but you will have to have tons of them before it is noticeable.

Consider using the Remove Unused Usings command in Visual Studio, which will remove using statements for namespaces that are not actually used. I use it often, but just so that the files are neat and not for performance.

+2
source

The compiler does an optimization for us to include the associated assemblies of namespaces where required , so you don't need a wrapper for optimization. It is best to include the entire namespace required by the class using using statement on the top .

0
source

You can use, it doesn’t matter, it doesn’t affect your performance (The best practice for your code is to delete without using the right button)

Everything loads when necessary.

0
source

When you use System.IO; at the beginning of the file, you tell the C # compiler to read this namespace to look for class names. It does not affect program performance at all.

But if you create wrappers in the same file, you will create an additional (albeit small) copy of the class for each class used, and this will degrade performance at runtime.

0
source

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


All Articles