Click here to Skip to main content
15,906,625 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
while importing namespace why we import sub namespace although we have imported parent namespace.......ex-
using system;
using system.data;
We hv imported system,again we r importing data why???????,plz share if any idea ........thanx in advance
Posted
Updated 13-Dec-11 3:20am
v2
Comments
Sergey Alexandrovich Kryukov 13-Dec-11 8:39am    
Who told you so?
--SA

1 solution

First, we are not "importing" anything here. The "using" clause is a pure syntactic sugar used to resolve the names, if you will. Also, name spaces themselves have nothing to do with relationship between assemblies — you can have different name spaces in the same assembly; and, in this case, will you say that the assembly "imports" itself once and then again? :-) All essential stuff happens only when you add a reference to a project.

So, first, this is strictly the same:
C#
using System.Data;

//...

DataTable myTable;
and
C#
System.Data.DataTable myTable;


Secondly, using the type in this example is only possible when the assembly "System.Data.dll" (found in the CAG) is referenced by an assembly you build.

Finally, having a common part "System" in the name spaces like "System" and "System.Data" says absolutely nothing about their relationship. They might be related or they might be not. Name spaces naming is totally unrelated. As I say, you can have different name space in the same assembly, but you also can use the same name space in totally unrelated assembly. For example, you can name your own name space "System.Data" and use it with the one from the assembly "System.Data.dll". Names of the name spaces are grouped only for convenience for only one purpose: name resolution.

—SA
 
Share this answer
 
Comments
Himu from Orissa 13-Dec-11 8:48am    
what we actually mean by "using system.data"?
Sergey Alexandrovich Kryukov 13-Dec-11 10:33am    
Almost nothing. It say to the compiler: you if don't find the declaration by non-qualified name, try to prefix if with "System.Data.". Nothing else! If the name space itself is not available if current or referenced assembly, the compiler reports error message.
--SA
Himu from Orissa 13-Dec-11 8:51am    
i mean if there is relation betn two,then is it necessary to use the sub namespace?
Sergey Alexandrovich Kryukov 13-Dec-11 10:36am    
In essence, one name space is not as sub-namespace of another. The relationship use purely syntactic.
If you write "using System" and not "using System.Data", you can use the naming "Data.DataTable". Can you get it finally?
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900