Using keyword

I

INeedADip

Are the using statements at the top of each .cs file mainly for UI and
type compatibility, or do they actually create dependencies for each
page.

When I create a new aspx page the following using statements are
included be default:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

I ask because I rarely use any System.Data objects. Is the compiler
smart enough to just "ignore" the using statment if I'm not actually
using anything from that namespace, or should I delete it for some
reason.

This isn't a problem by any means....I'm just curious.
 
J

Jon Skeet [C# MVP]

INeedADip said:
Are the using statements at the top of each .cs file mainly for UI and
type compatibility, or do they actually create dependencies for each
page.

All they do is stop you from having to type full namespaces. That's it.

(Short pedantic point of terminology: they're using directives. Using
*statements* are the ones which mirror a try/finally block with a
Dispose call.)
I ask because I rarely use any System.Data objects. Is the compiler
smart enough to just "ignore" the using statment if I'm not actually
using anything from that namespace, or should I delete it for some
reason.

It's not that it ignores it - it's that it doesn't make any difference
if you're not using any types in those namespaces.

Personally I prefer not to see using directives for namespaces I'm not
using - they should be a small hint to anyone reading the code as to
what namespaces they should expect to see used within the type.
 
M

Martijn Mulder

INeedADip said:
All they do is stop you from having to type full namespaces. That's it.
<snip>

I am not sure about this one but I think that a lot of 'using' statements on
top of the file makes compilation time longer since the compiler must search
different paths to find the class you are referring to while fully qualified
names (System.Windows.Forms.Form instead of using .... + Form) lead
directly to the desired class.
 
J

Jon Skeet [C# MVP]

Martijn Mulder said:
<snip>

I am not sure about this one but I think that a lot of 'using' statements on
top of the file makes compilation time longer since the compiler must search
different paths to find the class you are referring to while fully qualified
names (System.Windows.Forms.Form instead of using .... + Form) lead
directly to the desired class.

While it might contribute a small amount to the compilation time, I
doubt that it's a significant amount. There's no reason why the
compiler should need to do much work - it only needs to keep a lookup
table of type names, really.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top