Replies: 8 comments 16 replies
-
IMO where file-scoped namespaces are useful is in the 99.999% case where a file contains types (usually only one) within a single namespace. As such I don't think it would be overly restrictive if the language required that such namespace directives only appeared once and before other directives. I consider it more or less equivalent to Java |
Beta Was this translation helpful? Give feedback.
-
I'm excited for this one! I think it will make a significant change for the better. My vote goes on allowing one top-level namespace per file, but additional "traditional" braced namespaces may be added if desired, which are not nested inside the top-level namespace. Any code, including classes, "traditional" braced namespaces, and top-level statements, must appear after any top-level namespace. I can imagine the namespace declaration getting lost easily if it appears after any code. usings and a top-level namespace may appear in any order. namespace Foo;
void TopLevelMethod() { }
class Bar { }
namespace Foo.Baz
{
}
class Qux { } |
Beta Was this translation helpful? Give feedback.
-
I don't want to see much time is spent on this feature - it isn't such worthy. |
Beta Was this translation helpful? Give feedback.
-
namespace X;
class A {}
namespace Y;
class B {} My expectation on them is namespace X;
class A {}
namespace global::Y;
class B {} |
Beta Was this translation helpful? Give feedback.
-
Would this also allow top-level statements to be namespaced? I imagine that most of the time it's a non-issue but I've noticed that top-level statements in C# 9 can only go in the global namespace, which is something I don't like. This is not allowed: using System;
namespace MyProject
{
Console.WriteLine("Hello world");
} Would this? using System;
namespace MyProject;
Console.WriteLine("Hello world"); It feels like it should. |
Beta Was this translation helpful? Give feedback.
-
In writing .NET code for 10+ years, the only times I've ever seen more than one namespace in a single file were when the file was code generated, and even then it was extremely rare. There is absolutely no reason to support multiple top-level namespaces per file, it just is a recipe for confusion. FWIW, my original proposal for this issue from 2015 also was explicit in proposing that this should be disallowed. |
Beta Was this translation helpful? Give feedback.
-
It seems LDM did not talk about the resolution of |
Beta Was this translation helpful? Give feedback.
-
For anyone following this issue, we discussed it again on January 13th: #4328. |
Beta Was this translation helpful? Give feedback.
-
https://github.com/dotnet/csharplang/blob/master/meetings/2021/LDM-2021-01-05.md
Agenda
Beta Was this translation helpful? Give feedback.
All reactions