Is a method / variable reference in the external namespace more memory efficient than including it completely?

So, the next equivalent in memory usage?

System.Threading.Thread.Sleep(500);

vs

using System.Threading;
...
Thread.Sleep(500);

I would have thought that the less namespace you “use” in memory, the better, but I heard that the previous example should load the namespace into memory independently. I know that this Sleepis not the best example of a starving memory method, but it is just an example.

+3
source share
3 answers

The operator usingdoes not call anything at all loadable; it tells the compiler where to look for classes.

In two examples, the exact same code is generated.

+8
source

IL , .

. , /// .. (System.Threading) .

, , . # , , , - , , .

( ), ( ).

+3

, "using" "include" include plain C - CLR , .

using :

  • Tell the compiler where to look for classes (as directed by Guffa)
  • Make it easier for developers to declare classes - imagine that you need to enter the full class name in each declaration each time

The compiler will be responsible for its optimization, you have nothing to worry about.

+2
source

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


All Articles