C# compiler should be able to avoid error CS0118 in many cases #3669
Replies: 7 comments
-
Because when you're inside a namespace, you automatically get access to members of that namespace, but also to members of its parent namespaces (including the global namespace). |
Beta Was this translation helpful? Give feedback.
-
Right... That does seem intuitive and familiar, when formalised like that. I guess C# has worked like that since inception. I guess the part I'm not expecting is that this applies also to namespace "nodes" themselves (even ones that don't contain any types) and that by declaring Anyway, that's obviously by very old and very deliberate design, and I don't want to sidetrack the main point with this, so I'll update the issue. |
Beta Was this translation helpful? Give feedback.
-
This doesn't scale when you consider referring to nested symbols. For example: using OtherNamespace;
namespace Alpha.Beta.Namespace1
{
class SomeClass
{
void AMethod(Beta.TypeInsideBeta a) { } // Beta here is the namespace
void SomeMethod(Beta a) { } // (With the proposed change) Beta here is the type OtherNamespace.Beta
void OtherMethod(Gamma g) { } // Works fine
}
}
namespace Alpha.Beta
{
class TypeInsideBeta { }
}
namespace OtherNamespace
{
class Beta { }
class Gamma { }
}
You could argue that the compiler should search within the
I'd strongly recommend using type aliases for situations like this). |
Beta Was this translation helpful? Give feedback.
-
@DaRosenberg Preference aside. Why wouldn't you declare it inside
|
Beta Was this translation helpful? Give feedback.
-
@eyalsk thank you, that seems like a viable workaround actually! For my understanding, why does that work exactly? 😅 As an aside: while that does compile, it also yields a warning: |
Beta Was this translation helpful? Give feedback.
-
I'll just note that VB has this feature. We've jokingly referred to it as 'quantum namespaces'. Essentially because Visual Basic does not have the ability to nest namespaces (and therefore change the order in which they are considered) it must instead attempt to bind all namespaces/types in scope and only issues an error when the type/namespace does not match anything. Considering how complex namespace resolution already is I prefer the canonical solution to this proposal: move the using statements inside the namespace declaration. |
Beta Was this translation helpful? Give feedback.
-
That should only warn if you have that setting set. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
This is probably my number one nuisance with C# at the moment.
Consider this code:
Because
Beta
happens to form part of the current namespace, it cannot be used as a type.IMO this error makes no sense, because:
Beta
and the typeBeta
are technically in scope, specifying only a namespace without any type is not valid in the context (in fact, only places I can think of where it is valid, arenameof()
and thenamespace
declaration itself)Beta
is not a root namespace. Nor is it a child namespace of the current namespaceAlpha.Beta.Namespace1
. How come the namespaceBeta
is even considered in scope in this context in the first place?(Point 2 discussed in comments.)
Point 1 is the primary one though. Shouldn't the compiler be able to infer that I'm referring to the type, given that a namespace makes no sense in this location?
Beta Was this translation helpful? Give feedback.
All reactions