3

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,

Omar.M
  • 107
  • 10
  • It is customary to have interface names to start with the capital letter `I`, such as `IPayment`. This is exactly to avoid conflicts like this. – John Alexiou May 14 '23 at 06:24

3 Answers3

5

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.

Ofiris
  • 6,047
  • 6
  • 35
  • 58
  • Note that if you *need* namespaces to avoid name clashes, then maybe your names are bad. – Joey Mar 20 '16 at 10:16
  • Absolutely, I see no reason to have the same type name, especially for a class and an interface. – Ofiris Mar 20 '16 at 10:18
  • 1
    I was confused with the naming too, but i will change the interface name to IPayment. and change all affected areas. Thanks a lot – Omar.M Mar 20 '16 at 12:24
0

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

fozersahin
  • 98
  • 1
  • 5
0

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.