5.3. C# namespaces (Java packages)

Like the Java world, we always want to pack our C# classes into neat groups or packages. Besides making things much more organized, one important reason for packaging is to prevent naming conflicts when you are using classes from two separate developers who chose the same names for their classes.

In Java, you package classes using the package keyword. In C#, you use the namespace keyword to do exactly the same thing. In the example below, MyClass has been placed into a namespace called MyNameSpace.

1: using System;
2: namespace MyNameSpace{
3:   class MyClass{
4:     static void Main(string[] args){
5:     }
6:   }
7: }
					

When you want to access MyClass from another class, you can refer to it by its fully qualified class ...

Get From Java to C#: A Developer's Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.