3

I'm new to ASP.NET development and there is something I quite don't grasp regarding using directives...

using System;
using System.Data;
using System.Data.SqlClient;

In the above code example, I am curious as to why we must explicitly list System.Data and System.Data.SqlClient when they are already included in the first statement using System;. It seems redundant that we must specify namespaces that are included in parent namespaces. I know there is probably a very simple explanation to this question, but I've been unable to find it with my search.

Shandy Sulen
  • 301
  • 2
  • 17
  • 3
    It is like specifying several folders in your file system. Each namespace is unique. – ManoDestra Jul 12 '16 at 23:32
  • 1
    Possible duplicate of [Importing nested namespaces automatically in C#](http://stackoverflow.com/questions/9023465/importing-nested-namespaces-automatically-in-c-sharp) – Jonathon Reinhart Jul 12 '16 at 23:36
  • 1
    @ManoDestra Imagining it as a file directory system makes a bunch of sense – Shandy Sulen Jul 13 '16 at 14:23
  • Indeed. You could have a test.txt file in C:\files1 and one in C:\files1\backup. Both are different files, despite their names being identical. And the only thing that makes them unique is the directory path (or namespace in this case) that enables you to reach the specific file (or class in this case). – ManoDestra Jul 13 '16 at 14:31

1 Answers1

3
using System;

doesn't include the namespace System.Data. Those are separate namespaces. Thus, you need both if you intend to use classes defined in both of those namespaces.

Fang
  • 2,199
  • 4
  • 23
  • 44