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

79k views Asked by At

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 a custom tool for a CAD application. 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?

3

There are 3 answers

3
Sergei Rogovtcev On

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

4
Shadow Wizard Love Zelda On

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.

0
Nicola Musatti On

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.