为什么C#没有头文件?命名空间会处理所有事情吗? [英] Why doesn't C# have header files? Will the namespace take care of everything?

查看:564
本文介绍了为什么C#没有头文件?命名空间会处理所有事情吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一名新手程序员,有人可以清楚地说明C#中头文件和名称空间的用法吗?

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

因为在C ++中,我使用******.h文件读取库函数.当我看到一些用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?

我正在使用C#开发用于机械CAD软件的定制工具,每当我使用适当的功能打开文件(CAD文件)时,编译器给我一个错误,指出我提供的功能名称是在上下文中不可用.这里的上下文是什么意思?

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 ?

当我打开该CAD应用程序的帮助文件时,负责打开该文件的功能已在名为uf_part.h的头文件中提到.但是有一个名为NXopen的命名空间.

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.

我在Visual Basic中将名称空间用作using 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.

推荐答案

C#更加程序员友好".处理同一项目的文件时,它不会每次都手动指定头文件",而是会根据名称空间在所有项目文件中查找匹配项.

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. 在Visual Studio中启动新项目. (无论是哪种类型,无论是WinForms还是控制台)
  2. 右键单击该项目并添加新类.
  3. 在主类注释中,您可以看到刚刚添加的新类,而无需添加任何标题.

这是怎么做到的?只需对两个类使用相同的名称空间即可. .NET引擎足够聪明,可以将所有这些类链接在一起.

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.

现在,当涉及到外部代码时,意味着代码位于另一个DLL文件中,诀窍是添加对该DLL的引用(在Studio中->右键单击项目->添加引用- ->浏览),然后需要通过在顶部添加 using语句来指定要使用该DLL:

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;

就是这样.与C ++不同,您不需要.h文件,因为.NET将自动在引用的DLL文件中搜索匹配项.

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.

这篇关于为什么C#没有头文件?命名空间会处理所有事情吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆