Confusion in C # namespace structure

I am a little confused about the structure of the namespace in C # I am new to C # and WPF

When I create a new WPF project, by default these namespaces are included at the beginning

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; 

I am confused in understanding these structures. I understand that

 using System; 

means im refers to the System namespace

But what about others like

 using System.Text; 

What does it mean?

  • Am I referring to a single namespace named "System.Text"?
  • or am I referring to the Text namespace nested in the System namespace?

Also, if "Text" is nested inside the "System", why only "using System" includes all namespaces? should we explicitly include all the namespaces we use?

+4
source share
5 answers

Does using System.Text; that I use a single namespace with the name "System.Text", or that I use the namespace "Text" nested in the namespace "System"?

Last. You are using the Text namespace, which is nested inside the System namespace.

Please note that when you speak

 namespace Foo.Bar.Blah { class C { ... 

This is just a short way to write.

 namespace Foo { namespace Bar { namespace Blah { class C { ... 

Namespaces have only "simple" names.

Should there be an explicit "using" directive for each namespace used?

Usually yes. There are some situations in which you might want to not do this. For example, if you have two namespaces that contain a type of the same name, and you want to use types from both namespaces; in this case, it may be too confusing to β€œuse” the two namespaces. In addition, β€œuse” drives extension methods; if there are extension methods that you don’t want to communicate with, do not include the namespace that contains them.

if "Text" is nested inside the "System", why only "using System" includes all namespaces?

I do not understand the question as it is formulated. I think maybe you are asking:

Why using System; Doesn't Simple Text Name Allow Nested Namespace?

This is why this does not work:

 namespace Foo { namespace Bar { class Blah {} } } 

In another file:

 using Foo; class Abc : Bar.Blah {} 

using Foo; only includes types declared directly inside Foo. It does not contribute namespaces declared directly inside Foo. C # designers thought it was too difficult to put namespaces in scope; usually people use using directives to cast types to scope.

+15
source

Namespaces, by and large, are not hierarchical in meaning for the computer. Types under System.Text (for example, System.Text.StringBuilder ) are not considered to be located in System - so you will need to list them separately if you need types from both:

 using System; using System.Text; 

The only time they are considered as a hierarchy in relation to the compiler, which I can think of, one way or another, is when it comes to the namespace in which you declare something:

 namespace Foo.Bar.Baz { class Test { } } 

Inside Test it is as if you imported Foo , Foo.Bar and Foo.Bar.Baz .

Now, of course, namespaces form hierarchies from the point of view of human understanding - simplify the search for things. But the compiler and runtime do not use this hierarchical view of things in general.

+5
source

Do I need to explicitly include all the namespaces we use?

Yes or fully qualify type names.

2 namespace names

 using System; using System.Text; 

completely independent. Their names imply a relationship, but from outside those namespaces that can be considered symbolic.

You cannot use System.Text elements when you have only the first use.
And you do not need the first to use the second.

Only when you write code belonging to the nested namespace do you see some effect.

 namesapce A { namespace B { class Foo { } } namespace C { class Bar { private B.Foo f; } // B is a 'relative' ns } } 
+1
source

using System.Text imports all declarations inside System.Text , but nothing in its folders. There is no equivalent to System.Text.* , As in Java , as in an imaginary language.

An easy way to work with namespaces is to simply write your code in normal mode and CTRL +. above undefined keywords to tell VS.NET to automatically add a using link for you.

0
source

You get access to the text namespace nested in the system namespace. And yes, you need to include all the namespaces you use.

The namespace structure might look something like this:

 namespace Parent { namespace Child { public class ChildClass { } } } 
0
source

All Articles