Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Confusion about C# Namespace Structure

Tags:

namespaces

c#

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 ?

like image 862
ammar26 Avatar asked Sep 22 '11 14:09

ammar26


2 Answers

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".

like image 126
Eric Lippert Avatar answered Oct 05 '22 09:10

Eric Lippert


Namespaces are, by and large, not hierarchical in their meaning to the computer. The types under System.Text (e.g. 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.

like image 29
Jon Skeet Avatar answered Oct 05 '22 07:10

Jon Skeet