问题
I know that in java that we use *(asterisk) to import all the contents in a package like
import java.lang.*;
Then why don't we use same *(asterisk) in C# to import all the contents is there any method like in java to import all the contents. What is the difference between
import java.awt.*;
and
using System.windows.forms;
回答1:
What the Java import
does what .NET is called a reference - adding a reference to an assembly in .NET allows you to use the (public) types defined in that assembly.
The C# using directive is simply a way to access these types without typing out the whole namespace.
You can also use the directive to provide namespace aliases.
回答2:
In Java, without *, you have to import individual classes. In C#, using directives are for namespaces (but not any sub-namespaces, i.e., using System; doesn't get you System.Windows.Forms). So, import java.awt.* is giving you about the same as using System.Windows.Forms, so there's not really a need for * in .Net.
回答3:
Be clear about what Java's import
is doing. In spite of the name, no .class files are being loaded. All you're doing is saving yourself typing. Java import
allows you to refer to a class using its short name instead of its fully-resolved class name (e.g. String
instead of java.lang.String
).
C# using
is different from Java import
if it actually adds something to an assembly. Java .class byte code is loaded as needed by the class loader, not before.
来源:https://stackoverflow.com/questions/9769025/difference-between-c-sharp-using-and-java-import