tags:

views:

305

answers:

2

I was just wondering if there was a way in C# to import (use) everything inside a namespace like there is in java with the wildcard character.

import java.awt.*;
+11  A: 

That's what the normal using directive does. For instance:

using System;

means you can use Console, Guid, Int32 etc without qualification. The closest equivalent to the single import in Java is:

using Console = System.Console;

(etc)

but that's not used very often.

Jon Skeet
Thats something that I did not know could be done in C#, I kind-of like it because you only use Console but the syntax is really uncomfortable.
Lucas McCoy
It's more useful for getting round naming clashes, e.g. having "Button" imported from two different namespaces.
Jon Skeet
Or when your testers insist on having really long namespaces. :)
Steven Behnke
Aliases are also useful for shortening or clarifying long names, which sometimes happens with generics.
Hosam Aly
A: 

No, there is not a way to do something like:

using System.Windows.*;

I think the intent is to ensure that names are as clearly qualified as possible, and including something like this would potentially introduce more name collisions. Also, a "using" directive only changes how names are resolved... it does not cause additional assemblies (those that contain the classes of that namespace) to get loaded.

JaredReisinger
import java.awt.* imports all the classes in the java.awt package. Isn't that *exactly* what "using System.Windows;" would do for the System.Windows namespace?
Jon Skeet
It really depends on what you mean by "import", and by whether the "*" is recursive or not. (I don't know Java, so I'm coming at this from a completely C# perspective.) In C#, you can have a referenced assembly (from which you get the classes), and you also have "using" for name resolution only.
JaredReisinger
... Once an assembly is referenced in a project, you can use any of its classes without ever issuing a "using" statement.I guess my issue really was that I didn't know what Java's "import" really meant, and perhaps incorrectly assumed that it was more than C#'s "using".
JaredReisinger
@Jared: No, the "*" isn't recursive in Java, and in Java it's only used for name resolution too.
Jon Skeet
Sorry @Jared, but -1. Not only is this information incorrect, it's also misleading.
Hosam Aly