Namespaces in C# vs imports in Java and Python

后端 未结 6 1489
一整个雨季
一整个雨季 2021-02-20 03:21

In the Java and Python world, you look at a source file and know where all the imports come from (i.e. you know in which file the imported classes are defined). For example:

6条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-02-20 04:00

    How do I know where the Bar-class is declared? Does it come from the namespace foo, or bar, or anothernamespace? Visual Studio allows me to jump there, of course, but what if I am just taking a quick look at a source file in my editor?

    Essentially, you don't - but IntelliSense is helping. You cannot actually be sure by just taking a quick glance at the code, but you can hover over the symbol with your cursor, for example. But this is also possible in Python:

    from foobar import *
    from bazbaz import *
    
    a_bar = Bar()
    

    Where does Bar come from now?

    In C#, there does not seem to be such a convention for namespaces, or am I missing something? So, how do I know which directory and file to look in? (after figuring out which namespace the class came from).

    No, assemblies do not correspond to directory structures, which, IMHO, is a good thing. The solution explorer is offering a view of all the references added to your project. These references being assemblies, have a concrete representation as a PE file somewhere on your machine. You can easily look at the properties of a reference to see where the physical file is located.

    Edit: In order not to contradict other answers in this thread and create confusion: What I mean by saying assembly names do not correspond to directory names is that it is not actually enforced.

提交回复
热议问题