Difference between namespace in C# and package in Java

前端 未结 6 504
日久生厌
日久生厌 2020-11-28 20:32

What is the difference (in terms of use) between namespaces in C# and packages in Java?

相关标签:
6条回答
  • 2020-11-28 21:04

    There are a few details that differ.

    In Java the directory structure should match the package structure. No such restriction in C#.

    In C# you can have multiple namespaces in one file. In Java one file belongs to one package (see previous).

    Java has default/package accessibility. C# internal accessibility goes in assemblies.

    If you use VS and Eclipse and let them structure the project, then you will not feel the differences much.

    0 讨论(0)
  • 2020-11-28 21:04

    In C++/C#, namespaces are just used for partitioning names to avoid collisions by accidentally using the same name for a variable in different places.

    In Java, packages are far more than just that - packages are used for modules, the naming aspect is just a part of it.

    0 讨论(0)
  • 2020-11-28 21:09

    There's no such term as "namespace" in Java - a package acts as a namespace in Java though, in terms of providing a scope for names. It's also part of the accessibility model.

    From section 7 of the Java Language Specification:

    Programs are organized as sets of packages. Each package has its own set of names for types, which helps to prevent name conflicts. A top level type is accessible (§6.6) outside the package that declares it only if the type is declared public.

    EDIT: Okay, after the clarification: a Java package is similar to a C# namespace - except that it has an impact on accessibility, whereas in C# namespaces and accessibility are entirely orthogonal.

    0 讨论(0)
  • 2020-11-28 21:14

    A namespace is just like a new folder, all subfolders are sub-namespaces. If we consider a namespace as a function like we have a namespace advertising under marketing namespace then we use marketing.advertising.adsclass.adsmethod. Very easy to solve a problem. Java has same method via package but complex for new comers.

    In C#

    ''' namespace marketing{

               class admissions{
                         int admissions_method(){
                         }
               }
         namespace advertising{
               class  advertisement{
                        void ad_in_newspaper( int no_of_lines){
                        }
                        void ad_on_tv(int seconds){
                        }
    
         }
    

    }

    To use in client class

    using marketing;
    using marketing.advertising;
    

    ''' In java you use same method. You pack multiple classes in one package and use it many times. It increases uniqueness. You write once and use many times. Related classes in one package. No need to code many times.

    0 讨论(0)
  • 2020-11-28 21:26

    In java you can apply various access specifiers to classes which will have impact on your packages.

    protected : accessible to same package and to its subclasses in another package, default : accessible to same package, public : universally accessible, private : not even accessible by the same package.

    These type of access specifiers are not applicable to namespace in c sharp

    0 讨论(0)
  • 2020-11-28 21:30

    From: http://www.javacamp.org/javavscsharp/namespace.html


    Java

    Packages are used to organize files or public types to avoid type conflicts. Package constructs can be mapped to a file system.

    system.security.cryptography.AsymmetricAlgorithm aa;
    

    may be replaced:

    import system.security.Crypography; 
    class xxx { ...
    AsymmetricAlgorithm aa;
    

    There is no alias for packages. You have to use import statement or fully-qualified name to mention the specific type.

    package n1.n2;
        class A {}
        class B {}
    

    or

    package n1.n2;
       class A {}
    

    Another source file:

    package n1.n2;
       class B {}
    

    Package cannot be nested. One source file can only have one package statement.

    C#

    Namespaces are used to organize programs, both as an "internal" organization system for a program, and as an "external" organization system.

    System.Security.Cryptography.AsymmetricAlgorithm aa;
    

    may be replaced:

    using System.Security.Crypography; 
    AsymmetricAlgorithm aa;
    

    Alternatively, one could specify an alias for the the namespace, eg

    using myAlias = System.Security.Crypography; 
    

    and then refer to the class with

    myAlias.AsymmetricAlgorithm 
    
    namespace N1.N2
    {
        class A {}
        class B {}
    }
    

    or

    namespace N1
    {
        namespace N2
        {
            class A {}
            class B {}
        }
    }
    
    0 讨论(0)
提交回复
热议问题