Omar.M
Omar.M

Reputation: 107

Can I have a Class and Interface with same name in same namespace?

I was assigned on a project (C# based) to work on, and i found there is a class and interface with same name in the same namespace [class : Payment, Interface : Payment]. so when i build the solution it gives me error that it is not allowed.

Renaming one of them will affect other many areas in the solution. Actually I don't know how was it working!!

Any ideas?

Thanks,

Upvotes: 3

Views: 2067

Answers (3)

Anuj Chauhan
Anuj Chauhan

Reputation: 33

I also got an old project with this issue, where the same name is used for an Interface and a class in the same namespace.

It was in .net 4.5, but I don't remember any .net framework ever allowed this.

I am too very surprised, at how this was working earlier. Will investigate more and come back here for the knowledge base.

Upvotes: 0

fozersahin
fozersahin

Reputation: 98

Here is a nice explanation for your question. It's about Java but I think best practices can be similar about naming.

Java Interfaces/Implementation naming convention

Upvotes: 0

Ofiris
Ofiris

Reputation: 6151

It's not possible to have two types with the same name under the same namespace (this is one reason why namespaces are useful).

A good convention is to have the letter I before interface names, see: IEnumerable, ISerializable...

If you must keep the identical names, you may change the namespace of one of the types and change the using namespace declaration accordingly.

Upvotes: 5

Related Questions