How to handle same class name in different namespaces?

e4rthdog picture e4rthdog · Nov 12, 2012 · Viewed 30.7k times · Source

I am trying to create a common library structure. I am doing this by creating separate projects for every common lib I want.

I have the following 2 namespaces: MyCompany.ERP and MyCompany.Barcode

I need both of them to have a class named Utilities and be static. If I do that I will then need to specify the full namespace name before my static class in order to access it.

Is there any other preferred way to do it?

Or I should go for different names in classes like BarcodeUtils and ERPUtils?

Answer

Oded picture Oded · Nov 12, 2012

If i do that i will then need to specify the full namespace name before my static class in order to access it?

No, there is no need for that, though the details depend on the class that will use these types and the using declarations it has.

If you only use one of the namespaces in the class, there is no ambiguity and you can go ahead and use the type.

If you use both of the namespaces, you will either have to fully qualify the usages, or use namespace/type aliases to disambiguate the types.

using ERPUtils = MyCompany.ERP.Utilities;
using BCUtils = MyCompany.Barcode.Utilities;

public void MyMethod()
{
  var a = ERPUtils.Method();
  var b = BCUtils.Method();
}