开发者

Namespaces to avoid conflicts - example?

开发者 https://www.devze.com 2023-01-21 16:55 出处:网络
I read that way back programmers have to think of special names for their classes in order to do not conflict with another one when the file got loaded on users PC. That is what I do not understand, i

I read that way back programmers have to think of special names for their classes in order to do not conflict with another one when the file got loaded on users PC. That is what I do not understand, if the class was within e.g. DLL, how it could collide with 开发者_高级运维other class on that PC?

Even without namespaces, if I import a DLL, I guess I would need to call the class from that DLL so I could not make the code impossible to complile. I would really appreciate explanation here, thanks!


example: System.Drawing.Point and System.Windows.Point

So if a program references both assemblies, without the namespaces, the compiler will get confused when you declare Point p; or Point p = new Point(1,1);, for example


Consider if there are no namespaces. Then you load a type MyClass from an assembly. Now if you load another type from another assembly and there is a MyClass in there. How do you load the second type? How to do tell the compiler which one you want when you say

MyClass o = new MyClass() 

The answer - you have to name namespaces to uniquely identify the class, otherwise it's ambiguous. So you say why not limit the name space to the assembly. This is fine, however it appears that is such a great idea that the designers of the platform introduced a concept where anyone can create multiple namespaces within an assembly to allow people to partition their code better. Then we ask why not allow namespaces to go across assemblies so that we can partition the code more easily.

You have many uses for namespaces and it's upto you the app designer to come up with something that works for you - even if its only one namespace.


DLL Hell, the Inside Story is a good summary of the old issues - C# addressed this issuer per design - so you do not need to worry anymore.

0

精彩评论

暂无评论...
验证码 换一张
取 消