Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

is it a good idea to have classes with the same name in different namespace in c#?

for example, we have two classes for parsing the resumes, one to parse Excel and the other to parse HTML.what my colleagues love to do is to name these two classes the same name and put them into different namespace,like shown as below:

namespace XX.ResumeParsers.Excel
class ResumeParser{}

namespace XX.ResumeParsers.Html
class ResumeParser{}

I feel it is not a good idea, I'll prefer to rename the classes, and put them into one namespace(but in different files if needed):

//in Excel folder under ResumeParsers folder
namespace XX.ResumeParsers
class ExcelResumeParser{}

//in Html folder under ResumeParsers folder
namespace XX.ResumeParsers
class HtmlResumeParser{}

thus, the Hierarchy still exists in the folder, but the namespace is the same(do not match the folder hierarchy exactly), is that OK?

and if I am right, any idea how to persuade my colleagues? or is there any significant drawback in their solution?

thanks.

like image 832
John Cai Avatar asked Sep 14 '10 08:09

John Cai


People also ask

Can two namespace have same name?

namespace definitionMultiple namespace blocks with the same name are allowed. All declarations within those blocks are declared in the named scope.

Can I have class with same name as namespace?

Inside a namespace, no two classes can have the same name.

Do partial classes have to be in same namespace?

Every part of the partial class definition should be in the same assembly and namespace, but you can use a different source file name.

What is the difference between namespace and namespace name?

Names are what can be traditionally thought of as "variables". Namespaces are the places where names live. Typically this is a module. Each module (file) has it's own namespace -- Or it's own set of names which map to a corresponding value ( a->1, b -> 2 in my example above).


1 Answers

It's not usually a good idea, no - particularly if you need to use both classes from the same code. Whether they should be in different namespaces or not is a separate decision, but I'd definitely call them HtmlResumeParser and ExcelResumeParser instead of trying to make the namespace communicate context. It will make it much easier to determine exactly what you're talking about when reading code.

like image 168
Jon Skeet Avatar answered Sep 30 '22 20:09

Jon Skeet