In java, you can use 'import javax.swing.*'
to import multiple libraries in Java. Can you do the same thing in C#?
-
2Related: http://stackoverflow.com/questions/9023465/importing-nested-namespaces-automatically-in-c-sharp – tnw Jun 03 '14 at 18:40
-
You should try out the suggestions and weigh in - I'm getting killed here because I'm disagreeing with a 308k member. – Dave Doknjas Jun 03 '14 at 19:06
-
@DaveDoknjas, I dont think you're getting killed because you're disagreeing w/ a 308K member. Reed's answer is solid, clear and unambiguous. – crthompson Jun 03 '14 at 19:08
-
@DaveDoknjas then the solution would be to edit your answer to be more clear. Show use cases, documentation, build it up. The SO community is very good at upvoting the good and downvoting the wrong. – crthompson Jun 03 '14 at 19:38
-
You could start by looking over @tnw's link – crthompson Jun 03 '14 at 19:40
-
Thanks for the advice - I expanded my answer a bit - note that tnw's link is about importing namespaces in C# - this has nothing to do with the "import foo.bar.*" syntax in Java, which *only* makes types available, not other packages. – Dave Doknjas Jun 03 '14 at 19:46
2 Answers
The C# using
directive only allows you to "import" a single namespace at a time. There is no option to use using
with wildcards or similar.
Note that C#'s using directive already imports everything in the entire namespace by default. This means that using System.Collections.Generic;
is very similar to import java.util.*;
A single type can be handled via an alias: using StringList = System.Collections.Generic.List<string>;
This is really a difference in how these work at the language level. Given the C# assembly and namespace conventions, this is typically not an issue in practice. Note that C# uses assembly references to actually "import" the libraries, and the using
directive is merely a tool to simplify how you use the library types within your source code.

- 554,122
- 78
- 1,158
- 1,373
-
That is also how the Java "import foo.bar.*" syntax works - you have access to all the types within the package foo.bar. So the C# equivalent is just "using javax.swing" (assuming that "javax.swing" were available from C#, which it's not). – Dave Doknjas Jun 03 '14 at 18:39
-
@DaveDoknjas Yes, true - still can't "import multiple things at once", but I did edit to clarify – Reed Copsey Jun 03 '14 at 18:52
-
I think the op means importing multiple types - don't you think? Especially considering that "imports foo.bar.*" only imports all types within the foo.bar package, not other packages. – Dave Doknjas Jun 03 '14 at 18:55
-
@DaveDoknjas I have no idea - he mentioned "multiple libraries", which in C# is done via the project system ;) I suspect the OP doesn't understand how the C# project system with assembly references and `using` works in general, since it is different than Java. – Reed Copsey Jun 03 '14 at 18:58
-
But you do know that "import foo.bar.*" in Java *only* imports types on demand? This is exactly what "using foo.bar" does in C#. – Dave Doknjas Jun 03 '14 at 19:00
-
@DaveDoknjas Yes, but its still not the same as C#. If you leave out the assembly reference in the C# project (or don't include it on the command line for csc), you'll get nothing in C#... – Reed Copsey Jun 03 '14 at 19:03
-
We're only talking about language syntax here - you also can't control access to other packages purely at the language level in Java either - the packages have to be available. – Dave Doknjas Jun 03 '14 at 19:05
That is the way C# works, but different syntax:
using javax.swing;
This gives you direct access to all the types in javax.swing (not that you'd be able to do that but you get the idea).
Some documentation:
On the Java side: "To import all the types contained in a particular package, use the import statement with the asterisk (*) wildcard character ... Now you can refer to any class or interface in the graphics package by its simple name": http://docs.oracle.com/javase/tutorial/java/package/usepkgs.html
On the C# side: "To allow the use of types in a namespace so that you do not have to qualify the use of a type in that namespace ... Create a using directive to use the types in a namespace without having to specify the namespace": http://msdn.microsoft.com/en-us/library/sf0df423.aspx

- 6,394
- 1
- 15
- 28
-
-
-
Consider that if I `import System` I still dont get access to `System.Linq` – crthompson Jun 03 '14 at 18:42
-
@paqogomez: You get access to the types in System, which is the same as what "imports foo.*" gives you in Java. – Dave Doknjas Jun 03 '14 at 18:44
-
The downvote is unjustified - "using foo.bar" serves the same purpose in C# as "imports foo.bar.*" in Java. You can test this easily. – Dave Doknjas Jun 03 '14 at 18:46
-
I didnt downvote, but "importing multiple libraries" is not what `using foo.bar` does. – crthompson Jun 03 '14 at 18:50
-
See the syntax specs for Java - the only purpose of "imports foo.bar.*" is to make all the types within "foo.bar" available as needed - exactly the same as "using foo.bar" in C#. – Dave Doknjas Jun 03 '14 at 19:01