using namespaces

  • Thread starter Thread starter Davíð Þórisson
  • Start date Start date
D

Davíð Þórisson

I've never understood fully, why do I have to td eg:
using system;
using system.data;

I would have thought that the top line imports system and all of its
submembers, including data??
 
The goal of importing a namespace into a file is to increase readability,
having it recurse down an entire namespace hierachy defeats that purpose...

Imagine the following

using System;

namespace test{
public class utility{
Button b = new Button();
}
}

How would you ever know (or the compiler) whether that was
System.Windows.Forms.Button or System.Web.UI.WebControls.Button ??

Karl
 
thank you Karl
my question though is more concerning why I have to import both the top
level and also sub level namespaces eg I would have thought
using System;

would import all sub-namespaces eg. System.Data;

BTW, if the object - lets say the button you refer to - is not specified by
importing the according namespace, would that return an error or would ASP
try to guess which one I'm refering to??


"Karl"
 
I think my original reply answered your question. If by importing System it
also imported System.Data, System.Web, System.Web.UI,
System.Web.UI.WebControls, System.Windows, System.Windows.Forms and
everything else you'd defeat the purpose of the using statement. There
are a number of comflicting class names throughout the System namespace (and
others) and only specifying a specific namespace can resolve this ambiguity.

If the object is not specified by importnig the appropriate namespace, the
code won't compile. you cannot have any ambiguity when refering to
objects...the namespace must be clear and well defined. In the example I
gave bellow, I won't even be able to build the code.

Something else ot keep in mind is that you don't need to use using, for
example, the following is valid:

using System;
using System.Web.UI.Webcontrols;

public System.Windows.Form.Button b1= new System.Windows.Form.Button();
public Button b2 = new Button(); //refers to
System.Web.UI.WebControls.Button

or you could use aliases:

using form = System.Windows.Form;
using web = System.Web.UI.WebControls;

public form.Button b1 = new form.Button();
public web.Button b2 = new web.Button();


But again, all this only works because importing a namespace doesn't
automatically import everything beneath it...it would be chaos (absolute and
total chaos!!) if it did that ;)

Karl
 
You are assuming there is a namespace hierarchy with system being on the top
level and system.data one level down, something similar to a file system
where directory \temp\work is located under directory \temp. This assumption
is not correct. system is located in file system.dll and system.data in
system.data.dll. They are not parts of the same hierarchy, rather two
separate libraries. Therefore, when you declare system, it has nothing to do
with system.data.

Eliyahu
 
Davíð Þórisson said:
I've never understood fully, why do I have to td eg:
using system;
using system.data;

I would have thought that the top line imports system and all of its
submembers, including data??

You don't specifically *need* to use the "using" statement, you can also
use the full classname. "using" is just there so you don't have to specify
the namespace of the classes you want to use.

Don't confuse "using" with "project references" which you *do* need.
Here you don't point to a specific namespace but to an assembly instead.
There might be some relation between assembly name and (some) contained
namespace, but that's "coincidence". An assembly can contain any number
of namespaces.
When you have a reference to an assembly, you can use any (public) class
in it, by specifying the full classname.

Hans Kesting
 
Back
Top