简体   繁体   中英

Confusion about C# Namespace Structure

I am little confused about the structure of Namespace in C# I'm new to C# and WPF

When i create a new project of WPF, by default these Namespaces are included at the top

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'm confused understanding the structures of these. I understand that

using System;

Means that im referring to System namespace

But what about the others like

using System.Text;

What that means?

  • I'm accessing a single namespace named as "System.Text" ?
  • or I'm accessing "Text" namespace nested inside "System" namespace ?

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

Does using System.Text; mean that I'm using a single namespace named as "System.Text" or that I'm using the "Text" namespace nested inside "System" namespace?

The latter. You are using the "Text" namespace that is nested inside the "System" namespace.

Note that when you say

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

That is just a short form way of writing

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

Namespaces only have "simple" names.

should we have an explicit 'using' directive for every namespace we use?

Typically you do, yes. There are some situations in which you might want to not do so. For example, if you have two namespaces that both contain a type of the same name and you want to use types from both namespaces; in that case it might be too confusing to have a "using" of two namespaces. Also, "using" brings extension methods into play; if there are extension methods that you do not want to bind to, do not include the namespace that contains them.

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

I do not understand the question the way it is worded. I think maybe you are asking:

Why does using System; not make the simple name Text resolve to the nested namespace?

That is, why does this not work:

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

In a different file:

using Foo;
class Abc : Bar.Blah {}

The using Foo; only includes the types declared directly inside Foo. It does not bring in namespaces declared directly inside Foo. The designers of C# thought that it was too confusing to bring namespaces "into scope" like this; typically people use using directives to bring types "into scope".

Namespaces are, by and large, not hierarchical in their meaning to the computer. The types under System.Text (eg System.Text.StringBuilder ) are not considered to be in System - so you'd need to list them separately if you wanted types from both:

using System;
using System.Text;

The only time when they're considered as a hierarchy as far as the compiler is concerned - that I can think of, anyway - is when it comes to the namespace you declare something in:

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

Within Test , it's as if you've imported Foo , Foo.Bar and Foo.Bar.Baz .

Now of course namespaces do form hierarchies from a human understanding point of view - the make it easy to find things. But the compiler and runtime don't use that hierarchical view of things, by and large.

should we have to explicitly include every namespace we use ?

Yes, or fully qualify the type-names.

The 2 namespace usings

using System;
using System.Text;

are totally independent. Their names suggest a relation but from outside those namespaces that can be considered symbolical.

You cannot use members of System.Text when you only have the first using.
And you don't need the first in order to use the second.

Only when you write code that belongs to a nested namespace you see some effect.

namesapce A
{
    namespace B
    {
        class Foo { }
    }

    namespace C
    {
        class Bar { private B.Foo f; }  // B is a 'relative' ns
    }
}

using System.Text imports all declarations inside System.Text , but nothing in its folders. There's no System.Text.* equivalent like in Java like in an imaginary language.

An easy way to deal with namespaces is to just write your code normally and CTRL+. over undefined keywords to tell VS.NET to automatically add the using reference for you.

You are accessing the Text Namespace nested inside the System Namespace. And yes, you need to include every namespace you use.

A namespace structure can look something like the following:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM