为什么C#没有头文件?命名空间会处理一切吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11814771/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why doesn't C# have header files? Will the namespace take care of everything?
提问by Senthur
I'm a novice programmer, can anyone tell clearly about the usage of header files and namespaces in C#?
我是一个新手程序员,谁能说清楚 C# 中头文件和命名空间的用法?
Because in C++ I was using ******.hfiles to read library functions. And when I saw some sample programs in C# they were missing, Can anyone tell me why?
因为在 C++ 中,我使用******.h文件来读取库函数。当我在 C# 中看到一些示例程序时,它们不见了,谁能告诉我为什么?
I'm using C# to develop customised tool for a mechanical CAD software, there whenever I use the appropriate function to open the file (CAD file), the compiler is giving me an error stating that the function names which I supply are not available in the context. Here what does meant by context ?
我正在使用 C# 为机械 CAD 软件开发自定义工具,每当我使用适当的函数打开文件(CAD 文件)时,编译器都会给我一个错误,指出我提供的函数名称在上下文。这里的上下文是什么意思?
When I opened the help file of that CAD application the function which is responsible for opening the file has bee mentioned under a header file called uf_part.h. But there is an namespace called NXopen.
当我打开那个 CAD 应用程序的帮助文件时,负责打开文件的函数在名为uf_part.h. 但是有一个名为NXopen.
I used the namespace as using NXopenin Visual Basic, isn't that enough? DO I need to supply that header file as well ? If so how ? Kindly help me.
我using NXopen在 Visual Basic 中使用了命名空间,这还不够吗?我还需要提供那个头文件吗?如果是这样怎么办?请帮助我。
回答by Serg Rogovtsev
There's no such thing as header file in .net, because all needed metadata is contained in referenced assembly itself.
.net 中没有头文件之类的东西,因为所有需要的元数据都包含在引用的程序集本身中。
Have you referenced needed assembly in you project? Also please mind that there's no such thing as "function" in C#, only class methods (which means that you have to specify object or static class in you call).
您是否在项目中引用了所需的程序集?另请注意,C# 中没有“函数”这样的东西,只有类方法(这意味着您必须在调用中指定对象或静态类)。
Also: General Structure of a C# Program
另外:C# 程序的一般结构
回答by Shadow Wizard is Ear For You
C# is more "programmer friendly". When dealing with files of the same project, instead of manually specifying "header file" every time, it will go and look in all the project files for a match according to the namespace.
C# 更“程序员友好”。在处理同一个项目的文件时,不是每次都手动指定“头文件”,而是根据命名空间去查找所有项目文件中的匹配项。
To understand this, do the following steps:
要理解这一点,请执行以下步骤:
- Start new project in Visual Studio. (No matter what type, WinForms or Console)
- Right click the project and add new class.
- In your main class note you can see the new class you just added, without adding any header.
- 在 Visual Studio 中启动新项目。(无论是什么类型,WinForms 或 Console)
- 右键单击项目并添加新类。
- 在您的主课堂笔记中,您可以看到刚刚添加的新课程,而无需添加任何标题。
How this is done? Simply by having the same namespace to both classes. The .NET engine is smart enough to link all those classes together.
这是怎么做的?只需对两个类具有相同的命名空间即可。.NET 引擎足够智能,可以将所有这些类链接在一起。
Now, when it comes to externalcode meaning code sitting in a different DLL file the trick is to add reference to that DLL (in Studio --> Right click project --> Add reference --> Browse) then you need to specify you are going to use that DLL by adding a using statementon top:
现在,当涉及到外部代码意味着代码位于不同的 DLL 文件中时,诀窍是添加对该 DLL 的引用(在 Studio --> 右键单击项目 --> 添加引用 --> 浏览)然后您需要指定您将通过在顶部添加using 语句来使用该 DLL :
using ExternalDllName.ExternalNamespace;
That's about it. Unlike C++ you don't need to have .hfile as .NET will automatically search the referenced DLL files for a match.
就是这样。与 C++ 不同,您不需要.h文件,因为 .NET 会自动搜索引用的 DLL 文件以查找匹配项。
回答by Nicola Musatti
Compilers for modern languages, such as C# or Java store within compiled files information on the classes and methods they contain, and this information can be used to check the correctness of calls made from one source file to another or to library classes.
现代语言(例如 C# 或 Java)的编译器将有关它们包含的类和方法的信息存储在编译文件中,这些信息可用于检查从一个源文件到另一个或库类的调用的正确性。
When C was invented disk space, memory and CPU power were precious resources and this approach would not have been possible. Header files were introduced to allow the compiler to check that different source files conformed to the same interface. When C++ was invented the approach described above could have been possible, but I guess that it was chosen to stick to the C one for compatibility reasons.
当 C 被发明时,磁盘空间、内存和 CPU 能力是宝贵的资源,这种方法是不可能的。引入了头文件以允许编译器检查不同的源文件是否符合相同的接口。当 C++ 被发明时,上面描述的方法是可能的,但我想它是出于兼容性原因选择坚持使用 C 的。

