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?

Daniel A. White
  • 187,200
  • 47
  • 362
  • 445
  • Why exactly do you want to use `using static` with `List`? Do you know what `using static` is used for? (Hint: Read the documentation: https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/using-directive#static-modifier) –  Sep 24 '22 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;`). 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. – Jeroen Mostert Sep 24 '22 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;` (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. – Jeroen Mostert Sep 24 '22 at 19:57

0 Answers0