1

C# has an using static directive since C# 6.0 which is allows to includes more specific things (like classes, interfaces etc.) than namespaces. Well, I tried to include generic type class (System.Collections.Generic.List<T> to be clear). I used using static System.Collections.Generic.List<T>; and program failed. Is there a way to include generic type classes by using using static directive?

3
  • Why exactly do you want to use using static with List<T>? Do you know what using static is used for? (Hint: Read the documentation: learn.microsoft.com/en-us/dotnet/csharp/language-reference/…) Commented Sep 24, 2022 at 19:47
  • It is in fact possible to use generics in a using static, but only by instantiating them (using static System.Collections.Generic.List<int>;). In the specific case of List this is almost certainly not meaningful. You may be confusing this application of using with how Java deals with imports, but that's not what using static is for. Commented Sep 24, 2022 at 19:51
  • The closest you can get to making only specific types available is aliasing, with something like using IntList = System.Collections.Generic.List<int>; (again, this requires instantiating in the case of generics). This is generally intended only to resolve name clashes when they do occur, not as a way to pick and choose from namespaces, which the C# designers didn't consider useful. Commented Sep 24, 2022 at 19:57

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.