Note that there are some explanatory texts on larger screens.

plurals
  1. POName conflict between namespace and class
    text
    copied!<p>I have a structure with several namespaces, and in each namespace I have one static class that is sort of a small, little spider in the net of that namespace. Like the head of the department.</p> <p>Let's take an example of two namespaces, <code>Foo.Bar.Baz</code> and <code>Foo.Bar.Quux</code>. </p> <p>I have one file which looks like this:</p> <pre><code>namespace Foo.Bar.Baz { static class Baz { public static void DoSomething() { Console.Writeline("Doing something..."); } } } </code></pre> <p>And then a second file:</p> <pre><code>using Foo.Bar.Baz namespace Foo.Bar.Quux { static class Quux { public static void GetToWork() { Baz.DoSomething(); } } } </code></pre> <p>Now, I get an error in the second file when I try to compile because it thinks that Baz is the namespace and not the class. But since I have the line <code>using Foo.Bar.Baz</code> it should be fine to use any class inside that namespace without needing to prefix it with the name of the namespace, right? I can use other stuff from that namespace correctly, but not a class with the same name.</p> <p>It kinda feels like the compiler secretly adds this line to every file: <code>using Foo.Bar</code>.</p> <p>Also, I cannot just put the static <code>Baz</code> class inside the <code>Foo.Bar</code> namespace since then I will get errors saying that <code>Foo.Bar.Baz</code> is not a namespace (since I have other stuff in that namespace and have files that use that namespace). Then it seems the compiler sees the static class and decides that <code>Foo.Bar.Baz</code> is a static class and not a namespace.</p> <p>Of course, I could just rename the static classes to something like Manager but then I would still have to spell out the full <code>Baz.Manager.SomeMethod</code> and <code>Quux.Manager.SomeMethod</code> in files that need to access stuff from both namespaces. This feels rather clumpsy, I'd rather just have it be <code>Baz.SomeMethod</code> and <code>Quuz.SomeMethod</code>.</p> <p>Any ideas?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload