Replies: 16 comments
-
I might suggest a change to the name of this proposal/discussion to make it clear that it's referring to the |
Beta Was this translation helpful? Give feedback.
-
using SCG = System.Collections.Generic; // file-global scope
namespace X {
class Alpha {
using AQS = SCG.Stack<SCG.Queue<Action>>;
// ...
} // class Alpha
class Beta {
Alpha.AQS actions; // uses alias 'AQS' defined above in class Alpha
} // class Beta
} // namespace X
[AliasExportToProject]
using SCG = System.Collections.Generic; // file-global scope This would allow to use SCG in all files of the same project without redefining it in every file. [AliasExportToSolution]
using SCG = System.Collections.Generic; // file-global scope This would allow to use SCG in all files of all projects of the same solution without redefining it in every file. namespace XYZ {
[AliasExportToNamespaceUse]
using SCG = System.Collections.Generic; // namespace scope
} // namespace XYZ This would allow to use SCG in all files of all projects of all solutions, but only in those scopes where I think these additional 'exports' could cover all cases, where C# using alias is more limited than C/C++ |
Beta Was this translation helpful? Give feedback.
-
I think you should open another issue for that feature since it's divergent to the subject of this thread. |
Beta Was this translation helpful? Give feedback.
-
I kind of like this. I can see where this would be beneficial where you have type name collisions and you want to be specific with a type in a block of code. I can also see where this would be confusing. Interesting idea. |
Beta Was this translation helpful? Give feedback.
-
I wouldn't have it on namespace level, because it implies that the imported types and namespaces are available to the same namespace in another file, too, even if the using directive is not repeated there. |
Beta Was this translation helpful? Give feedback.
-
That's been permitted since C# 1.0. |
Beta Was this translation helpful? Give feedback.
-
@HaloFour Okay, never used it and never seen it actually... |
Beta Was this translation helpful? Give feedback.
-
All built-in templates in VS use global using directives. plus roslyn. What IDE do you use? |
Beta Was this translation helpful? Give feedback.
-
Global using, before the namespace declaration, but not after it. |
Beta Was this translation helpful? Give feedback.
-
Just to be clear, that's why it's called using "directive" and not declaration. It only affects the current file. |
Beta Was this translation helpful? Give feedback.
-
But isn't the intention of this proposal to scope that directive to its block? I'd find it confusing when being used within namespace scope. That's probably why I haven't seen it being used by others yet. If this feature were available I would make use of it primarily on subclasses and methods. |
Beta Was this translation helpful? Give feedback.
-
Namespace-level using directives already have a scope: the enclosing namespace. namespace A { using System; }
namespace B { /* System is not imported here. */ } The intention of this proposal is to extend that to other blocks, in addition to namespace's. |
Beta Was this translation helpful? Give feedback.
-
#133 should respect this proposal as well. |
Beta Was this translation helpful? Give feedback.
-
Now I know 😄 Nevertheless I find that confusing and won't use it. But this proposal I really like. |
Beta Was this translation helpful? Give feedback.
-
To not conflict with implicitly scoped usings (#1174), we probably want something to disambiguate. like void M() {
using namespace My.Extensions;
using static System.Console;
using alias MyType = Some.Ambiguous.Type;
} Since this is a statement-level directive, that way it stands out as a directive among statements. |
Beta Was this translation helpful? Give feedback.
-
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Originally proposed at dotnet/roslyn#10299, dotnet/roslyn#4491
The idea is to be able to use
using
directives in any block,This would enable (1) type-scoped type aliases and (2) not importing types to the whole file when they are just required in a particular block.
Closer usings should take precedence in binding as they already do for global and namespace-level usings.
Open question: should we also support
enum
blocks in addition to class, struct and interface?Since we do have a championed proposal for enum members (#297), I think it would make sense to have it.
Beta Was this translation helpful? Give feedback.
All reactions