Difference between namespace in C# and packages in Java


Packages in Java

Packages are used in Java in order to prevent naming conflicts, to control access, to make searching/locating and usage of classes, interfaces, enumerations and annotations easier, etc.

A namespace is designed for providing a way to keep one set of names separate from another. The class names declared in one namespace does not conflict with the same class names declared in another.

Define a Package as −

package package_name;

Restrict the access of classes (or class members) to the classes within the same package, but in C# with namespaces you cannot achieve this.

Namespace in C#

A namespace is designed for providing a way to keep one set of names separate from another. The class names declared in one namespace does not conflict with the same class names declared in another.

Define a Namespace as −

namespace namespace_name {
   // code declarations
}

In Java, the directory structure should match the package structure, but not required in C#.

In C#, add multiple namespaces in one file, whereas in Java, a file belongs to a package.

Samual Sam
Samual Sam

Learning faster. Every day.

Updated on: 23-Jun-2020

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements