begginers question about C#.
In every program I have to include several namespaces, like:
using System;
using System.Collections.Generic;
using Syste
System
and System.IO
namespaces are different.
You can treat "subnamespace" as parent-child relationship in the object model. If you have access to the "Car" object does not mean that you have access to car's wheels.
System
is a huge namespace that contains hundreds of nested namespace and thousands of classes. You should specify all nested namespaces separately to state what part of the module are you interested in.
Since you're beginner let me clarify one thing namespace
in C# and package
in Java are different things. no need to merge them.
System
and System.Text
are two different namespaces. That System.Text
seems to be a part of System
is the semantics we as programmer put into it. There's no such thing as a nested namespace from a platform view;
But even if that was not the case what should happen if you had
namespace MySystem{
namespace Foo{
class Bar {...}
}
class Bar{...}
}
using MySystem;
class MyClass{
private Bar _myBar; //Which one is it MySystem.Foo.Bar or MySystem.Bar?
}
Because nested namespaces are not included with parent one. See using directive documentation for details
A using directive does not give you access to any namespaces that are nested in the namespace you specify.
Imagine these namespaces with these classes (the last name is a class):
A.B.Class1
A.Class2
Now you have the following:
using A
- allows you to refer to Class2 directly... but not to Class1.
using A.B
- allows you to refer to Class1 directly but not to Class2.
If you want to refer to both classes directly in your code, you need both usings.
Not all classes in .net is inside one big container.Doing so increases the chance of class name collision and it doesn't look good in the first place.Namespaces are containers that try to keep your library clean and make more sense.Having a FTP class and String class together under one container does not logically make any sense.They do two different things and they should be kept in separate containers.
Also a namespace can be nested.Sometime A namespace can all but have just another namespace,without any class.so to access a class you need to qualify the full namespace before you can use it.
In your case you use different classes ,which are on different containers.So if you need them you need to qualify them with their namespace.