Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why Import Child Namespace if Parent Namespace Imported?

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 Answers

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.

like image 131
lavinio Avatar answered Dec 11 '25 03:12

lavinio



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!