Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c#: Namespace precedence / priority? Have 2 classes with the same name

I have 2 classes, both are called the same "Product", 1 class is mine and the other belongs to somebody else.

I get a compile error in code as it appear some code is trying to use the Product from "my" class rather than "their".

I have temporary fixed this by prefixing there namespace on the lines that use the class.

I have the "using" of their class.

So whats the way to fix this, leave my prefix or reorder the namespaces... whcih should come first?

I suppose this gives me another problem... If i fix this issue and i need to reference my class then i am going to need to prefix that.

Any advise the best way to tackle this would be very helpful.

like image 786
Martin Avatar asked Mar 25 '11 11:03

Martin


2 Answers

You can be explicit in your declarations, for example.

var myProduct = new MyCompany.MyLibrary.Product();
var otherProduct = new OtherCompany.OtherLibrary.Product();

You can even use the following :

using myNS = MyCompany.MyLibrary;
...
var myProduct = new myNS.Product();
like image 79
Richard Friend Avatar answered Nov 01 '22 21:11

Richard Friend


You can also use a namespace alias directive.

using myProduct = SomeNameSpace.My;
using thierProduct = SomeNameSpace.Thier;
like image 29
Mark Coleman Avatar answered Nov 01 '22 22:11

Mark Coleman