views:

275

answers:

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.

+13  A: 

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.

Martinho Fernandes
automagically, nice one.
tafa
A: 

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

leppie
+4  A: 

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.

Andrew Hare
A: 

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.

David Basarab
+1  A: 

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.

casperOne
+3  A: 

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.

joshua.ewer
Never underestimate the `OutOfMemoryExceptions` that occur in the head of the developer.
bzlm
+1 nice analogy
Chris Ballance
+2  A: 

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.

pinkeerach