Difference between c# using and Java import

10,766

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.

Share:
10,766
Prabhavith
Author by

Prabhavith

Hi I am Prabhavith working in Cognizant Technology Solutions

Updated on June 14, 2022

Comments

  • Prabhavith
    Prabhavith almost 2 years

    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;
    
  • marcus
    marcus about 7 years
    I don't think this is right regarding Java. In Java import is a way to type less too. Getting the compiler to see the packages and classes is done elsewhere: on the command line, in project files (Maven, Ant, something IDE-specific), etc.
  • David A. Gray
    David A. Gray over 6 years
    I suspect we're splitting hairs here, but a C# using adds little to the assembly, unless you count that it imposes a requirement that the implementing assembly must be named as a reference. So far as I can determine, it's the reference that actually modifies the assembly. That being so, it appears to me that C# using and Java import are esseintially different names for the same critter.
  • David A. Gray
    David A. Gray over 6 years
    If I am reading it 5 years later, it matters to me, and probably to others in search of the same information that brought me to this page today. Moreover, I also think it's important for readers to understand that adding a using declaration is not sufficient to alter the assembly in any meaningful way without the appropriate assembly in the list of references. That's what goes into the manifest, which is, so far as I can tell, the only way that either can be said to affect the assembly, since using is just source code.