O'Reilly logo

From Java to C#: A Developer's Guide by Heng Ngee Mok

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required