Search code examples
c#nestednamespacessystem

Dumb C# Question, How is System and System.Text both namespaces


How is System and System.Text both namespaces


Are they created as like nested namespaces

namespace System{
    Some Classes with Methods
    
    namespace Text{
        Some Classes with Methods
    }

    namespace Linq{
        Some Classes with Methods
    }
}

because text is nested inside of system you have to call it like

using System.Text;

----------------OR-----------------

are they created like

namespace System{
    Some Classes with Methods
}

namespace System.Text{
    Some Classes with Methods
}

namespace System.Linq{
    Some Classes with Methods
}

so when you call it you are calling it by it's actual namespace

using System.Text;

--------------OR----------------

or maybe it happens because it is written like

namespace System{
    Some Classes with Methods
}

using System;
namespace Text{
    Some Classes with Methods
}

using System;
namespace Linq{
    Some Classes with Methods
}

so because Text is using System you have to call it like

using System.Text;

-----------------OR--------------

Possibly some other way that I am not smart enough to think of


Solution

  • Your first and second will behave the same. Namespaces are a bit like folders and classes are a bit like files. You could have your namespaces in a hierarchy like folders:

    namespace System{
      namespace Text{
        class StringBuilder{
    

    If you were trying to imagine this as a sequence of DOS commands it might be like every namespace is a mkdir, every { is a cd into it and every } is a cd .. out of it :

    mkdir System               (Mkdir doesn't crash if a directory exists)
      cd System
      mkdir Text
        cd Text
        edit StringBuilder.cs
    

    You could specify the full path in one hit:

    namespace System.Text{
      class StringBuilder{
    

    Which could be like

    mkdir System\Text         (Yes, mkdir can accept a full path and will create every directory along the way)
      cd System\Text
      edit StringBuilder.cs
    

    Your third is legal C# but not the same as the other two. Your third declares 3 root level namespaces called System, Text and Linq. A using statement is not like a DOS cd that "leaves you inside namespace X" it's more like "add namespace X to the search path so if you're looking for class Z you have another known path to search"

    In DOS terms your third example is like

    mkdir System
      cd System
      cd ..
    
    path add System
    
    mkdir Text
      cd Text
      cd ..
    
    path add Text
    
    mkdir Linq
      cd Linq 
      cd ..
    

    You don't end up with a hierarchy of folders that way, you end up with 3 folders in C:\

    You could write

    var sb = new StringBuilder();
    

    But it would be a Text.StringBuilder not a System.Text.StringBuilder because it would be in c:\Text\StringBuilder.cs - it'd still be found when searched for because of the "path add Text" but adding a folder to a search path does not cause folders to be created inside other folders

    The file/folder analogy eventually falls down because C# can have any number of classes inside any number of namespaces inside a single file named nothing to do with anything - heck, it could be Cdrive.cs and contain hundreds of classes and namespaces, but for this purpose it'll do..