Tech Off Thread

7 posts

Forum Read Only

This forum has been made read only by the site admins. No new threads or comments can be added.

Namespace declaration in C#

Back to Forum: Tech Off
  • User profile image
    oskari

    Hi,

    I have a suggestion for the C# language team. As you know, a .cs file often contains code in only a single namespace. It would be nice if I could simply say

    namespace foo;

    class bar
    {
        ...
    }
     ...

    instead of

    namespace foo
    {
        class bar
        {
            ...
        }
        ...
    }

    The default namespace for the file would be changed, replacing the global namespace. This would make things simpler, help to reduce the } } } mess, and save a tab level.

    Or is there a reason for why this is not a good idea?


  • User profile image
    object88

    Here's a reason:  In the example below, what is the namespace of class B?

    namespace Level1;

    class A
    {
       ...
    }

    namespace Level2
    {
       namespace Level3
       {
          class B
          {
             ...
          }
       }
    }

    At first glance, it looks like B resolves to Level2.Level3.B, which is wouldn't.  It's just cleaner if eveything is clearly outlined.

  • User profile image
    oskari

    It's Level1.Level2.Level3.B obviously. I don't think there's any problem in that. And how many times do you have several top level namespaces in a one file anyway? I have no such files myself.

  • User profile image
    object88

    In my limited experience, I've never had more than one namespace in a .cs file.  I'm just saying that it might be misconstrued, given my example.  If the .cs file was very long, and class B was at the bottom of the file, the reader might miss it.  Someone working with command-line compilers and basic text-editors would have a harder time.

  • User profile image
    LazyCoder

    How will this type of shortcut reconcile in a partial class?

  • User profile image
    miies

    I don't even put more than one class/module/whatever in a file. One class per file, use good file names and you'll know exactly where to look for a specific piece of code - at least, that's my experience so far.

  • User profile image
    lars

    Anything that increases code readability is a Good Thing. Let the curly braces be your friends.

    /Lars.

Conversation locked

This conversation has been locked by the site admins. No new comments can be made.