e4rthdog
e4rthdog

Reputation: 5223

How to handle same class name in different namespaces?

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?

Upvotes: 40

Views: 49634

Answers (8)

millimoose
millimoose

Reputation: 39950

The MS guidelines have the following to say:

Do not introduce generic type names such as Element, Node, Log, and Message. There is a very high probability it would lead to type name conflicts in common scenarios.

and

Do not give the same name to types in namespaces within a single application model.

I concur that it's probably a good idea to use BarcodeUtilities and ErpUtilities instead. (Unless the utility classes are not meant to be used by client code, in which case you could name them Utilities and make them internal.)

Upvotes: 12

Oded
Oded

Reputation: 498972

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();
}

Upvotes: 67

kkocabiyik
kkocabiyik

Reputation: 4406

It actually depends on the purpose of your classes. If you are going to distribute your Barcode.Utilities and ERP.Utilies seperately it is better stay like this. On the other hand, if you are going to use them only in same class, you may use 2. method for easiness of code.

Upvotes: 0

Matthew Watson
Matthew Watson

Reputation: 109567

"Utilities" is not a very good name for a class, since it is far too generic. Therefore, I think you should rename both of them to something more informative.

Upvotes: 8

Johnny_D
Johnny_D

Reputation: 4652

There isn't any other way. You can make an aliases in using directives:

using MC=MyCompany.ERP;
using MB=MyCompany.Barcode;
...
public void Test()
{
  var a = MC.Utilities.Method();
  var b = MB.Utilities.Method();
}

It's the simplest way to manage them.

Upvotes: 22

Matt Varblow
Matt Varblow

Reputation: 7881

I would suggest using different class names. If you really want to call both of them Utilities then you could use the alias feature on the using directive, e.g.

using ERP = MyCompany.ERP;
using Barcode = MyCompany.Barcode;

...
    ERP.Utilities.SomeMethod();
    Barcode.Utilities.SomeMethod();

Upvotes: 1

Wouter de Kort
Wouter de Kort

Reputation: 39898

You will have to use the full path when both are named the same. Otherwise you will get an ambiguous reference error.

You can use an alias however that will save you some typing:

using Project = PC.MyCompany.Project;

I would go for a different name that's somewhat more descriptive. A

Upvotes: 0

user3378
user3378

Reputation:

You can use an alias:

using BarcodeUtils  =  MyCompany.Barcode.Utilities;

on the pages you have clashes. But ideally rename them if this is happening in a lot of places.

Upvotes: 4

Related Questions