This project is read-only.

File scoped namespaces.

Topics: C# Language Design
May 6, 2014 at 5:14 AM
Every file in a project that utilizes namespacing contains a namespace line near the top and a beginning/ending curly brace (1-2 lines depending on brace style). Along with this, if you follow the most common C# indentation style (and the one that ships with Visual Studio), the vast majority of your code is indented once. This extra indentation does not yield any value in terms of readability in projects that follow the one-namespace-per-file pattern.

Proposed solution:
Allow a file-level namespace declaration that looks something like this:
namespace Foo.Bar;
Putting this at the top of the file would cause the entire file to be considered inside of that namespace.

Should the line be required to be at the top of the file or allowed anywhere? If allowed only as the first non-comment/non-blank line the logic would be simple, everything in the file is considered to be within the given namespace. If it is allowed anywhere, then it either should still apply to the whole file (and there can only be one such line) or it applies to everything below the line it is on until the next such namespace declaration (or end-of-file).

Personally, I think allowing it only as the first line (or perhaps before the first class declaration so it can be below the using statements if desired) would reduce the chance of unexpected behavior when working with large files. Also, the parsing required by the compiler would be slightly simpler.

Adding this to the language shouldn't be too complicated and I will volunteer to submit a pull request, though I would rather not go to the work without first getting the feature approved (at least as something that is desirable).
May 6, 2014 at 8:56 AM
Personally I was always use this one indentation and without it code looks unreadable for me because code lies very close to left side where I have many controls of VisualStudio and R#.

Personally I will never use it.
May 6, 2014 at 2:41 PM
Wouldn't this be solved just by changing your indentation style to something like the following?
Code in braces is always indented once, except for namespace declarations, which are not indented.
This would result in code like:
using System;

namespace Example.HelloWorld
class Program
    void Main()
        Console.WriteLine("Hello World");
It looks somewhat ugly, because it's not consistent, but I think it solves your issue and it doesn't require any change in the language.
May 6, 2014 at 3:06 PM
Edited May 6, 2014 at 3:07 PM
I'm strongly in favor of adding this. I've always thought requiring all the code to be inside an (indented) namespace block was bizarre(1). Java's package syntax is far nicer. The namespace syntax is a constant annoyance because you lose one indentation worth of line readability meaning less stuff fits on the screen. I do not like @svick's suggestion as it looks very ugly, makes the code harder to read, and would require special rules for just about any code formatter in existence.

I can count on one hand the number of times I've seen a single file contain more than 1 namespace, and in all cases, it was code generated by a template. Thus, I think allowing a namespace at the top of the file to be applied to all classes in that file would save boilerplate for the vast majority of cases and make code more readable by allowing more to fit on the screen. I think the only requirement should be that the namespace declaration occurs anywhere before the first class declaration. That seems the least ambiguous to me.

1- Ok, so technically a namespace isn't required, but not having one is terrible style.
Apr 15, 2015 at 7:49 PM
Edited Apr 15, 2015 at 7:56 PM
Actually, this could as well be valid code:
using System
namespace MySpace
class Program
  void Main
    with Console
        WriteLine "Hello World"
and the compiler could possibly be made so it would compile both original and this .... because:
`using` DotWord (';' | Eol) ==> usingStatement
`namespace` Word ('{' | ';' )  ==> namespace declaration or block
`class` Word ClassSpec? ('{' | INDENT + not getOrSet ) ==> class with either c-style {block} or python/boo indented block (lookahed not to find property)
Type Name ( ArgList | INDENT ) ==> method with body
Type Name NODENT ==> field (where NODENT = new line + same indentation or look-ahead DEDENT without consuming it)
Type Name '{'? ( 'get' | 'set' ) ==> property
the above is something like grammar description that could be used by some parser generator (ANTLR; of course with some modification)
these are just ideas, but I would really love such wrist-friendly universal compiler
Apr 16, 2015 at 1:30 AM
Discussion have moved to