newbie question re namespaces

  • Thread starter Thread starter Laurel
  • Start date Start date
L

Laurel

If I have "using System;" at the top of my code, why do I also need "using
System.IO;" or "using System.Xml;" to reference attributes of System.IO and
System.Xml?

In other words, why can't I just use "using System," and then the following?
(I get a compile error if I try).
IO.StringReader strdr = new System.IO.StringReader(strXML);

Xml.XmlTextReader reader = new System.Xml.XmlTextReader(strdr);

Instead, I need to declare "using System.IO" and "using System.Xml" and
then the following format.

StringReader strdr = new System.IO.StringReader(strXML);

XmlTextReader reader = new System.Xml.XmlTextReader(strdr);



TIA - LAS
 
Short answer:

Everything comes from the System object.

If the compiler allowed you to only have to declare

using System;

It would have to compile and build into your project everything
contained under System.

This would make it a very big application.

Simply put, follow the rules of the code.
 
Laurel said:
If I have "using System;" at the top of my code, why do I also need
"using System.IO;" or "using System.Xml;" to reference attributes of
System.IO and System.Xml?

In other words, why can't I just use "using System," and then the
following? (I get a compile error if I try).
IO.StringReader strdr = new System.IO.StringReader(strXML);

Xml.XmlTextReader reader = new System.Xml.XmlTextReader(strdr);

Instead, I need to declare "using System.IO" and "using System.Xml" and
then the following format.

StringReader strdr = new System.IO.StringReader(strXML);

XmlTextReader reader = new System.Xml.XmlTextReader(strdr);

Because they are different namespaces. The using directive gives access to
the types in a namespace without having to prefix the type with the
namespace. It does not give you access to other namespaces, even those
namespaces that might happen to begin with the same prefix, as those are
still different namespaces. BTW, once you have "using System.IO" you only
need to do: StringReader strdr = new StringReader(strXML);.

Note that you can also use the "using" directive to alias a namespace or a
class. Ex:

using io = System.IO;
using xmlReader = System.Xml.XmlTextReader;

io.StringReader strdr = new io.StringReader(strXML);
xmlReader xread = new xmlReader(strdr);
 
Laurel said:
If I have "using System;" at the top of my code, why do I also need "using
System.IO;" or "using System.Xml;" to reference attributes of System.IO and
System.Xml?

They're different namespaces - you can't use a using directive to make
a whole hierarchy of namespaces available.
In other words, why can't I just use "using System," and then the following?
(I get a compile error if I try).
IO.StringReader strdr = new System.IO.StringReader(strXML);

Once you've done "using XXX" when an unqualified type name (eg YYY) is
used, if a type XXX.YYY is available, that will be used. You can't use
*part* of a namespace.
Xml.XmlTextReader reader = new System.Xml.XmlTextReader(strdr);

Instead, I need to declare "using System.IO" and "using System.Xml" and
then the following format.

StringReader strdr = new System.IO.StringReader(strXML);

XmlTextReader reader = new System.Xml.XmlTextReader(strdr);

No, having done "using System.IO;" you can do:

StringReader strdr = new StringReader (strXML);
XmlTextReader reader = new XmlTextReader (strdr);
 
See two questions below.

Tom said:
Short answer:

Everything comes from the System object.

If the compiler allowed you to only have to declare

using System;

It would have to compile and build into your project everything
contained under System.

But I do have a "using System" at the top. Is this making my code huge
already? I inherited the skeleton so have always done it this way.
This would make it a very big application.

Simply put, follow the rules of the code.

Not sure what you mean by this. Where do I find "the rules of the code?"
 
thanks - helpful
Tom Porterfield said:
Because they are different namespaces. The using directive gives access
to the types in a namespace without having to prefix the type with the
namespace. It does not give you access to other namespaces, even those
namespaces that might happen to begin with the same prefix, as those are
still different namespaces. BTW, once you have "using System.IO" you only
need to do: StringReader strdr = new StringReader(strXML);.

Note that you can also use the "using" directive to alias a namespace or a
class. Ex:

using io = System.IO;
using xmlReader = System.Xml.XmlTextReader;

io.StringReader strdr = new io.StringReader(strXML);
xmlReader xread = new xmlReader(strdr);
 
And thanks to you too.

Jon Skeet said:
They're different namespaces - you can't use a using directive to make
a whole hierarchy of namespaces available.


Once you've done "using XXX" when an unqualified type name (eg YYY) is
used, if a type XXX.YYY is available, that will be used. You can't use
*part* of a namespace.


No, having done "using System.IO;" you can do:

StringReader strdr = new StringReader (strXML);
XmlTextReader reader = new XmlTextReader (strdr);
 
Is this making my code huge already?
"using" (in this context) doesn't import things; it is simply a compiler
instruction to save you from having to type
System.IO.Something.SomethingElse.SomeClass each time.

This code is referenced (typically) from other assemblies, so there is no
compilation. It is JIT-compiled at runtime (not compile-time), but only on a
method-by-method bassis, so you never feel the pain of compiling everything.
In fact, references aren't resolved until needed, so even having lots of
references and "using" instructions does *nothing* (other than look ugly and
waste a little drive space) until those components are actually *used*.

Marc
 

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

Back
Top