Feature Request: Exclude class from namespace 'using' directive #1754
Replies: 5 comments
-
What makes this: using NamespaceOne except Foo;
using NamespaceTwo; better than what you can already do: using NamespaceOne;
using NamespaceTwo;
using Foo = NamespaceTwo.Foo; ? |
Beta Was this translation helpful? Give feedback.
-
@svick well for starters in my example 'Foo' is a class within Things also get weird when you need a majority of classes from an api are in the same namespace. It just seems unnecessary to include every single class you're using from one or the other namespace just to avoid an ambitious reference error. tl;dr it gets messy depending on the type. You also shouldn't be forced to change all your code when introducing a new library to your project because it's easier to just exclude the conflicting class than to deal with ambiguous reference errors by using fully qualified names when later you could easily remove that library and now you've already updated your code to deal with this issue. It's unnecessary. I'm sure other people have some examples, but it just seems like a nice QoL change that wouldn't break backwards compatibility. |
Beta Was this translation helpful? Give feedback.
-
It's like that in my example too.
You don't need to do that, what you need is a using alias directive (like the one in my example) for the ambiguous types. You don't need to include classes that are not ambiguous one by one.
You don't have to. If you know which class is ambiguous, you can use a using alias directive for that one class and you're done. |
Beta Was this translation helpful? Give feedback.
-
@svick so if I'm using multiple Dictionary<TKey, TVal>'s your solution is to create an alias for every conflicting one and be done? That's so unnecessary. |
Beta Was this translation helpful? Give feedback.
-
@exts Sorry, it wasn't clear to me that you're dealing with generics. In that case, my solution is to improve the using alias directive to support generics: #1239, instead of introducing a completely new concept to the language. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
There are times when you want to use a collection of classes, but there may be a chance that a class in that directive you don't want to use is included and conflicts with an existing included namespace. There should be some sort of syntatic sugar that allows c# to be smart enough to say "no don't include this at all" to avoid these problems.
Something as simple like:
using DirectiveName except Foo;
or
using DirectiveName except (Foo, Bar);
and if we have 3 classes 'Foo, Bar and Buzz' in that directive only Buzz gets included.Beta Was this translation helpful? Give feedback.
All reactions