更多的是出于好奇而不是任何问题,但为什么 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/