c# - 为什么 C# 为 `using` 定义了两种不同的用途?

标签 c# using language-specifications

更多的是出于好奇而不是任何问题,但为什么 C# 为关键字 using 定义了两个不同的“目的”?一方面,这是一个指令......

used to create an alias for a namespace or to import types defined in other namespaces.

另一方面,这是一个声明……

defines a scope, outside of which an object or objects will be disposed.

对我来说,同一个关键字似乎有不同的用法,但也许我遗漏了什么。这个关键字有两个不同的目的是有原因的吗?或者,在编译器的深处,这两个目的真的是一回事吗?

最佳答案

几年前我在 Eric Lippert 的博客上问过他同样的问题 here (见第一条评论)。

他的回答是:

This is a tricky point of language design; when one keyword is used to represent two completely different concepts, it can be confusing. But introducing a new keyword per concept makes the language feel a bit bloated. I personally would have chosen "imports" or some such syntax for the directive form to ensure that it is not confused with the statement form, but I understand that its a judgment call.

We were designing a feature for C# 4.0 that got cut which was yet another form of "partial" class; basically, a way to share attribute metadata between the machine-generated and user-generated halves of a partial class. I pushed back on using the keyword "partial" for the feature because we would then have had THREE subtly different meanings for "partial" in C#, which I felt was two too many. (I was advocating adding another conditional keyword "existing". Unfortunately the point ended up moot since the feature was cut for lack of time.) -- Eric

对于那些不知道 Eric 是谁的人,他是 C# 编译器团队的开发人员。

关于c# - 为什么 C# 为 `using` 定义了两种不同的用途?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5260915/

相关文章:

java - 为什么空数组引用的数组访问表达式不抛出 NullPointerException?

object - Kotlin中嵌套对象声明的可访问性不一致

C++ `using` 模板类中类型别名的命令

C# 仅在需要时有条件地处置 Entity Framework DbContext

c# - 使用 block : object initialization compiled into try block

python - False == 0 和 True == 1 是实现细节还是由语言保证?

c# - 如何在 C# 控制台应用程序中解锁 Eyeshot

c# - 如何在C# MailMessage中设置电子邮件过期时间

c# - 无法将泛型类型的对象转换为泛型接口(interface) C#

c# - 如何在c#中的backgroundworker中打开另一个窗体