views:

82

answers:

2

Why would someone have

Import System

as well as

Import System.Web.UI.WebControls

If System is root of all namespaces what is the harm in just having Import System in the code behind.

+1  A: 

Import System does not import the other, lower level namespaces. Each level must be imported explicitly.

Gary McGill
Is it correct to say that import would just import all the classes available in the namespace. However any further nested namespaces would have to be explicitly imported
Indeed. I couldn't have (and did not) put it better myself.
Gary McGill
+2  A: 

If import System did import all of the lower-level namespaces, then there would be almost no reason to have an import at all.

But the reason we do have a namespace system is to avoid name collisions where different packages contain classes of the same names.

For example, there could be two different namespaces, (making these up) System.Secure and System.Insecure. Both could have the classes for dealing with http with exactly the same names, but one could implement using secure but slower protocols, and the other insecure but quicker ones.

The developer could choose between them easily, without having to rename references all over the code. Or the code could be switched more easily at runtime, using reflection.

lavinio