Difference between namespace in C# and packages in Java

CsharpProgrammingServer Side Programming

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.

raja
Published on 17-Sep-2018 12:28:04
Advertisements