Can two classes with the same name be in the same namespace across two different assemblies?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



Can two classes with the same name be in the same namespace across two different assemblies?



I have two different assemblies: Assembly1 and Assembly2. Both assemblies use the same namespace SoftwareDrivers. Now, I have two implementations of a class that has identical functionality, just different implementation and dependencies that tie one implementation to Assembly1 and the other to Assembly2; I'll call this class PackageWriter.


Assembly1


Assembly2


SoftwareDrivers


Assembly1


Assembly2


PackageWriter



It is possibly, and likely, some projects will need to reference both assemblies, so I anticipate there will be an issue with resolving which PackageWriter to use in that scenario. Is this the case? If so, is the only way to resolve this issue, without changing the structure of the project, to change the name of PackageWriter?c


PackageWriter


PackageWriter





Why not simply try it out? IMHO it smells if two classes have identical names (and even identical namespaces) but do different things.
– HimBromBeere
Aug 7 at 14:57






extern alias. There are features that help you deal with these problems, but usually it's a sign that you're not picking good enough names. I.e. what actually distinguishes a PackageWriter that uses assembly1 from one that uses assembly2, and why isn't that part of the name?
– Damien_The_Unbeliever
Aug 7 at 15:03



extern alias


PackageWriter





If they have "identical functionality but different implementation", then why not just pick the one with the better implementation and use that in both places?
– Rufus L
Aug 7 at 15:34






@RufusL - if one should in fact be named XmlPackageWriter and the other ASN1PackageWriter, then neither is necessarily better than the other, but as I said above, it points to a naming failure.
– Damien_The_Unbeliever
Aug 7 at 16:21


XmlPackageWriter


ASN1PackageWriter





@Damien_The_Unbeliever I guess I don't understand why you'd have two different methods in the same namespace that have identical functionality. Maybe it should be phrased similar functionality?
– Rufus L
Aug 7 at 17:40





2 Answers
2



Yes they can. If both assemblies are referenced, you will receive an error similar to this:



error CS0433: The type 'Testing' exists in both 'MyApp.Domain, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' and 'MyApp.Infrastructure, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'


error CS0433: The type 'Testing' exists in both 'MyApp.Domain, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' and 'MyApp.Infrastructure, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'



Note, you should never purposefully put yourself in this situation. However, if you for some reason find yourself in a situation where you have to handle this:



Every assembly has an "alias" in .NET and by default it is global. This is usually invisible to the day to day programmer. You can change the alias by clicking on the reference and changing the Alias in Properties. You would then reference it by having extern alias myalias at the top of your file, and the actual class referenced like this myalias::MyApp.Testing


global


extern alias myalias


myalias::MyApp.Testing



This is perhaps needed when running 2 versions of an API or similar, although I maintain to avoid this if at all possible.



For more detail, see this answer.



If it's within the same namespace then NO unless you are declaring it as partial class


partial





partial would only work inside a single assembly. It's a compile time feature.
– Damien_The_Unbeliever
Aug 7 at 15:02






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

How to determine optimal route across keyboard