It will be great to have 'using' keyword in C# like in C++ - for declaring local type aliases in namespace, class/struct, method. Also with specifying access modifier, if defined in namespace or class/struct.
Fiona Niu[MSFT] on 11/13/2018, 11:11 PM (21 days ago):
Thank you for taking the time to provide your suggestion. We will do some preliminary checks to make sure we can proceed further. You will hear from us in about a week on our next steps.
Kendra Havens [MSFT] on 11/20/2018, 02:01 PM (15 days ago):
Vladyslav Prokopenko on 11/21/2018, 03:05 AM (14 days ago):
UPD. Something went wrong with formatting, code is displayed as plain text (at least for me). Sorry for that.
Unfortunately, no. Anyway thanks for your suggestion. I mean usage like the following examples: Any of the following applicable for classes nice to have also for interfaces. (In case realization allows.) 1) Namespace aliases in namespace or class:
Intent: allow avoid putting tons of using statements in the beginning of file, allow handy way for specifying exact class in case you have naming conflict (e.g. you have class CustomNS.StreamReader and use it in one file with System.IO.StreamReader).
using SX = System.Xml; //per-file namespace alias
namespace SomeNS
{
private using SIO = System.IO; /visible only in this namespace and nested entities/
public using ST = System.Threading; /available for namespace users/
public class /or interface/ SomeClass
{
public using TaskNS = System.Threading.Tasks; /available for class users/
private using Generics = System.Collections.Generic; /visible only for this class and nested entities/
}
}
2) Type aliases. Now you can define type alias only per file. I propose the following:
Intent: see 1), also you can hide actual type from your class user to achieve non-breaking change replacement of some used entities in case you keep the interface.
namespace SomeNamespace
{
public using Reader = System.IO.StreamReader; /available for namespace users/
private using StreamProvider = SomeCustomStreamWrapper; /visible only for this namespace and nested entities/
public class /or interface/ SomeClass
{
public using Collection = System.Collections.Generic.List; /with possibility specify generic parametr later; not exact syntax, just an example of usage/
public using IntCollection = System.Collections.Generic.List<int>;
/or even can be like
public using IntCollection = Collection<int>;/
}
public class /or interface/ SomeGeneric<T>
{
public using Param = T;
public using Collection<T> = System.Collections.Generic.List<T>;
}
}
in this example you can replace List with some other collection and user won't need to make changes in code.
3) Namespace member usings.
Intent: e.g. you want to widely use some class' static method, that called SomeNS.NestedNS.AnotherOneNS.OneMoreNS.SomePublicClass.NestedPublicClass, in just one method. To avoid writing this every time or putting using directive in the beginning of file it will be great to have the following.
Dylan Cottell on 11/21/2018, 05:58 AM (14 days ago):
#1 is already a feature that exists on a per-file basis.
With your example provided in #3, you could easily call a public static method within a public static class after aliasing the namespace.
What doesn't exist is the ability to add an access modifier to a using statement, meaning that you couldn't set an alias globally for a namespace. I personally cannot think of any use cases for this functionality, and might do more harm than good.
Hope this helps.
Vladyslav Prokopenko on 11/22/2018, 09:23 AM (13 days ago):
You are right about #1, but there was mentioned one more point - using in class. Talking about #3, aliasing in this case is really handy just in that case, we can create alias inside a class (or even method).
Dylan Cottell on 11/22/2018, 09:48 AM (13 days ago):
Ah, I understand now. #2 was a little confusing at first.
I like the idea of being able to set global aliases, or setting a using within a class. Although the use case isn't very clear to myself at this time, I could see some use for this. I'd argue that you'd be better instantiating the class rather than importing the namespace for use and simply use more of the namespace in your definition to differentiate the two classes, but for some very specific scenarios or for the sake of saving time I could see some potential value.
It will be great to have 'using' keyword in C# like in C++ - for declaring local type aliases in namespace, class/struct, method. Also with specifying access modifier, if defined in namespace or class/struct.
This issue has been moved from https://developercommunity.visualstudio.com/content/idea/381482/extend-using-keyword-functionality-c.html VSTS ticketId: 728129 These are the original issue comments:
Fiona Niu[MSFT] on 11/13/2018, 11:11 PM (21 days ago):
Thank you for taking the time to provide your suggestion. We will do some preliminary checks to make sure we can proceed further. You will hear from us in about a week on our next steps.
Kendra Havens [MSFT] on 11/20/2018, 02:01 PM (15 days ago):Does the using Statement in C# satisfy this request?
Vladyslav Prokopenko on 11/21/2018, 03:05 AM (14 days ago):
UPD. Something went wrong with formatting, code is displayed as plain text (at least for me). Sorry for that.
Unfortunately, no. Anyway thanks for your suggestion.
I mean usage like the following examples:
Any of the following applicable for classes nice to have also for interfaces. (In case realization allows.)
1) Namespace aliases in namespace or class:
Intent: allow avoid putting tons of using statements in the beginning of file, allow handy way for specifying exact class in case you have naming conflict (e.g. you have class CustomNS.StreamReader and use it in one file with System.IO.StreamReader).
2) Type aliases. Now you can define type alias only per file. I propose the following:
Intent: see 1), also you can hide actual type from your class user to achieve non-breaking change replacement of some used entities in case you keep the interface.
in this example you can replace List with some other collection and user won't need to make changes in code.
3) Namespace member usings.
Intent: e.g. you want to widely use some class' static method, that called SomeNS.NestedNS.AnotherOneNS.OneMoreNS.SomePublicClass.NestedPublicClass, in just one method. To avoid writing this every time or putting using directive in the beginning of file it will be great to have the following.
1) inspired by https://en.cppreference.com/w/cpp/language/namespace_alias
2) and 3) above inspired by 'using' keyword in C++ (https://en.cppreference.com/w/cpp/language/type_alias and https://en.cppreference.com/w/cpp/language/using_declaration)
Example code (due to broken markup)
1)
2)
3)
Dylan Cottell on 11/21/2018, 05:58 AM (14 days ago):
#1 is already a feature that exists on a per-file basis.
With your example provided in #3, you could easily call a public static method within a public static class after aliasing the namespace.
What doesn't exist is the ability to add an access modifier to a using statement, meaning that you couldn't set an alias globally for a namespace. I personally cannot think of any use cases for this functionality, and might do more harm than good.
Hope this helps.
Vladyslav Prokopenko on 11/22/2018, 09:23 AM (13 days ago):
You are right about #1, but there was mentioned one more point - using in class.
Talking about #3, aliasing in this case is really handy just in that case, we can create alias inside a class (or even method).
Dylan Cottell on 11/22/2018, 09:48 AM (13 days ago):
Ah, I understand now. #2 was a little confusing at first.
I like the idea of being able to set global aliases, or setting a using within a class. Although the use case isn't very clear to myself at this time, I could see some use for this. I'd argue that you'd be better instantiating the class rather than importing the namespace for use and simply use more of the namespace in your definition to differentiate the two classes, but for some very specific scenarios or for the sake of saving time I could see some potential value.