Does `Using Namespace;` consume more memory?
Asked Answered
R

7

7

Does Using Namespace; consume more memory?

I'm currently working on a mobile application and I was just curious if those unneeded using statements that visual studio places when creating a class make my application require some extra memory to run.

Regenerative answered 17/2, 2009 at 18:23 Comment(0)
P
15

To put it simply: no.

Those statements aren't translated into any form of IL. They're just shortcuts to avoid using (ugly!) fully qualified type names. But, if you're using VS2008 and/or R# you can remove unused ones automagically.

Petunia answered 17/2, 2009 at 18:24 Comment(0)
M
5

Namespaces are a compile-time only feature of C# that allow you to save time during development. The using directives are utilized by the compiler to look up shorthand Type names in your code.

Basically each time the compiler encounters a type name in your code that it does not know it takes each using directive and prepends it to the type's name and sees if that fully qualified name resolves.

Once you application is compiled the namespaces and the using directives are gone as the IL does not need them.

Meadowsweet answered 17/2, 2009 at 18:27 Comment(0)
L
3

If by "memory" you mean "I have to remember why I put those using statements at the top in the first place" then yes.

But, no not in the sense you mean. .NET assemblies are typically loaded on-demand, so you won't incur any performance penalty by having those extra statements. However, for the sake of maintainability, you'll want to remove them.

Lissotrichous answered 17/2, 2009 at 18:28 Comment(1)
Never underestimate the OutOfMemoryExceptions that occur in the head of the developer.Fruma
G
3

here's the microsoft information on the "using" keyword in this context: http://msdn.microsoft.com/en-us/library/sf0df423.aspx. it basically provides an alternative to using the fully qualified name on an assembly, but it doesn't actually import or include the assemblies in memory with that particular keyword.

Gratian answered 17/2, 2009 at 18:33 Comment(0)
S
2

No, it does not. It simply is a shorthand to allow you to use only the type name

StringBuilder

As opposed to the namespace-qualified type name:

System.Text.StringBuilder

In the file where the using statement is declared.

Schwa answered 17/2, 2009 at 18:27 Comment(0)
N
1

No, it will only make the compiler run a tiny bit slower :)

Nahum answered 17/2, 2009 at 18:26 Comment(0)
S
0

It does not. It is used so that you do not have to define the object you are using by your full name. Instead of System.IO.File you can just do File if you are using System.IO.

It is not loaded into the memory until you actually create the object. Unlike C++ the entire library is not imported, because the .NET framework is installed on the target machine and loaded in the GAC, and your custom .dll are in the bin folder of the application folder.

Savdeep answered 17/2, 2009 at 18:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.