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

Senthur picture Senthur · Aug 5, 2012 · Viewed 61.8k times · Source

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.

Answer

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.