Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 756
Senthur Avatar asked Aug 05 '12 07:08

Senthur


People also ask

Why there is no string in C?

There is no string type in C . You have to use char arrays. By the way your code will not work ,because the size of the array should allow for the whole array to fit in plus one additional zero terminating character.

Why C has no exception handling?

As such, C programming does not provide direct support for error handling but being a system programming language, it provides you access at lower level in the form of return values. Most of the C or even Unix function calls return -1 or NULL in case of any error and set an error code errno.

Why array bounds checking is not available in C?

This is due to the fact that C++ does not do bounds checking. Languages like Java and python have bounds checking so if you try to access an out of bounds element, they throw an error. C++ design principle was that it shouldn't be slower than the equivalent C code, and C doesn't do array bounds checking.

Why does C still exist?

The C programming language doesn't seem to have an expiration date. It's closeness to the hardware, great portability and deterministic usage of resources makes it ideal for low level development for such things as operating system kernels and embedded software.


2 Answers

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.

like image 159
Shadow Wizard Hates Omicron Avatar answered Oct 23 '22 22:10

Shadow Wizard Hates Omicron


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

like image 30
Serg Rogovtsev Avatar answered Oct 23 '22 23:10

Serg Rogovtsev