Difference between c# using and Java import

依然范特西╮ 提交于 2019-11-30 15:06:11

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!