4

so my program has these 2 lines at the beginning

using System;
using System.IO;

Question: Is the second statement actually necessary to include Sytem.IO methods and properties in my code? It seems that 'System.IO' is a 'child' of the namespace 'System'. Shouldn't the first line grab all the child namespaces too? Or Do I not understand namespaces correctly?

Logan Bender
  • 367
  • 1
  • 5
  • 12

4 Answers4

5

System.IO namespace is used for Input Output operations.(Ex: File Operations)

System namespace does not include all child namespaces.
So if you want to perform IO Operations you should include System.IO namespace explicitly.

First Question : Is the second statement actually necessary to include Sytem.IO methods and properties in my code?

Yes it is Necessary as System namespace does not include Child namespaces.

Second Question : It seems that 'System.IO' is a 'child' of the namespace 'System'.

Yes System.IO is a Child of System namespace.

Note : though System.IO is a child namspace of System, it will not be included when you include System namspace

Third Question : Shouldn't the first line grab all the child namespaces too? Or Do I not understand namespaces correctly?

No first line using System; does not grab all the Child namespaces as it is not java to import all child namspeaces using wild card character star *

Sudhakar Tillapudi
  • 25,935
  • 5
  • 37
  • 67
2

C# is not like java, where you can use wildcards to import namespaces.

using System;

in C# is not the same as

import system.*;

in Java.

And that's really all there is to it. You need to explicitly include namespaces - not much more to say about it :)

matt
  • 9,113
  • 3
  • 44
  • 46
0

Shouldn't the first line grab all the child namespaces too? Or Do I not understand namespaces correctly?

No - it will only make an alias for types in the System namespace directly. "Nested" namespaces (such as System.IO) are not made aliases automatically in C#. This is mentioned in the help for the using directive (italics added by me for emphasis):

allow the use of types in a namespace so that you do not have to qualify the use of a type in that namespace

You would still need to qualify IO for IO operations. For example, if you wanted to use the Path class, you'd need:

var filename = System.IO.Path.GetFilename(fullPath);
Reed Copsey
  • 554,122
  • 78
  • 1,158
  • 1,373
0

To expand on the other answers, there really aren't "child namespaces". There's nothing stopping you from declaring your own System.IO. You wouldn't want to of course, but you don't need a parent System namespace to do it.

You may have a project that by default has MyCompany.MyProject namespace, but you could just as easily declare XYZ.Some.Other.Random.Namespace in another .cs file of the same project. There's no inheritance to namespaces and XYZ.Some.Other doesn't exist. C# will let you use it, however, when you try to instantiate your class using the rest of it (Random.Namespace), it doesn't know how to find the class and won't resolve until you fully qualify the namespace in the using or fully qualify your variable declaration.

TyCobb
  • 8,909
  • 1
  • 33
  • 53