Sunday, February 17, 2008

How to avoid naming collisions in C#

In order to avoid naming collisions in C# you can use a namespace- or class alias.

When making a new instance of the ArrayList class you would usually write:

using System.Collections;


ArrayList data = new ArrayList();

However, if you already have another namespace imported with an ArrayList class you would get a naming conflict. To solve this you can add a namespace alias:

using Lists = System.Collections;


Lists.ArrayList data = new Lists.ArrayList();

You could also have a class alias like this:

using List = System.Collections.ArrayList;


List data = new List();