Why doesn't C# have header files? Will the namespace take care of everything?

血红的双手。 提交于 2019-12-18 14:14:07

问题


I'm a novice programmer, can anyone tell clearly about the usage of header files and namespaces in C#?

Because in C++ I was using ******.h files to read library functions. And when I saw some sample programs in C# they were missing, Can anyone tell me why?

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 ?

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.

I used the namespace as using NXopen in Visual Basic, isn't that enough? DO I need to supply that header file as well ? If so how ? Kindly help me.


回答1:


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.

To understand this, do the following steps:

  1. Start new project in Visual Studio. (No matter what type, WinForms or Console)
  2. Right click the project and add new class.
  3. In your main class note you can see the new class you just added, without adding any header.

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.

Now, when it comes to external code 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 statement on top:

using ExternalDllName.ExternalNamespace;

That's about it. Unlike C++ you don't need to have .h file as .NET will automatically search the referenced DLL files for a match.




回答2:


There's no such thing as header file in .net, because all needed metadata is contained in referenced assembly itself.

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).

Also: General Structure of a C# Program




回答3:


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.

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.



来源:https://stackoverflow.com/questions/11814771/why-doesnt-c-sharp-have-header-files-will-the-namespace-take-care-of-everythin

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!