Why does C# define two different uses for `using`?
Asked Answered
S

3

23

More a question out of curiosity than anything, but why does C# define two different "purposes" for the keyword using? On one hand, it's a directive...

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

On the other, it's a statement which...

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

To me, it seems like different uses for the same keyword, but maybe I'm missing something. Is there a reason why this keyword takes on two different purposes? Or, are both of these purposes, deep down in the belly of the compiler, really the same thing?

Stemware answered 10/3, 2011 at 14:19 Comment(13)
I've always wondered this too!Riddance
I think somebody on the keyword-naming team just had a brain fart when they chose the same word for two completely unrelated things :)Lahnda
There are some keywords that are used for more than one function. "where" is used for Generics and for LINQ.Klaxon
you can have notebook (computer) or notebook (pocketbook). The same word - two diffrent meaning vary depends on the context. This is how language works.Garey
What is the problem? Different meanings for different scopes.Streamlined
I'm pretty sure that the using statement and the using directive are two separate things, but yes, I would like to know why there isn't a with or use statement, or perhaps a import or uses directive insteadDebora
@SWeko: well, because in comparison to python, for example, using as a directive doesn't import anything. It just specifies the list of scopes that will be used as prefixes for all kind of objects that cannot be resolved in current scope.Martica
@zerkms: Import as a word means that some sort of merchandise is crossed across a border. It has no intrinsic value as a programming word, and in C/C++ import basically means 'paste the (object) code of the imported thing here'. In c# we could just call it namespace importer, not type importer, if that makes it better :) And also, let's not forget the third use of using, it's possible to define type aliases using the using directive.Debora
@SWeko: "In c# we could just call it namespace importer" --- that is what I said about, it is wrong. In c# nothing is imported. using System... is just a hint to compiler to add prefixes for all unresolved types of objects until it founds it. So it tries one by one using directive value and checks if it has found the correct existing type.Martica
@Linkgoron: not to be picky, but where (and also var, select, yield or from) are considered contextual keywords, they are not real reserved keywords like using and can, for instance, be used as a variable name without using the @-sign, resulting in allowed, but silly code like var var = 42.Particular
@Particular I'd bet that it's because they were added after C#2 and Microsoft didn't want to break backwards compatibility. Just look what happened with the async keyword. They had to add ANOTHER keyword to keep backwards compatibility.Klaxon
@Linkgoron: it is exactly that reason indeed, see also comment of Eric Lippert on page 65 in TCPL.Particular
@Hector fairly sure it must have been more deliberate than that - seeing as the statement was only introduced in C# 2.0, while the directive was there from 1.0.Mikelmikell
D
20

I asked Eric Lippert the same question on his blog a few years ago here (see the first comment).

His response was:

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

For those who don't know who Eric is, he's a developer for the C# compiler team.

Demodulate answered 10/3, 2011 at 14:28 Comment(1)
That's an interesting bit of insight into some of the decisions made with the language.Stemware
G
19

I cannot see how these two uses could be the same thing in the compiler.

I think the reason for the double use is simply the fact that the keyword fits very well for both of the purposes and there is no danger of ambiguity (one is directive, the other is statement, so they appear in completely different context). I think this is perfectly fine - a language should have a small number of keywords and their uses should be easy to understand - that's the case for both uses of using.

As Linkgorn mentioned in a comment, another example of the same situation is where which also exists in two places (generic constraints and LINQ query syntax).

Gazette answered 10/3, 2011 at 14:22 Comment(1)
another example is the out keyword which may also be used to declare covariance of a generic type parameters for interfaces and delegate types in C# 4Samford
R
3

First is that they do both fit the word, and since they both can't be used in the same context, they can't be confused for one another.

The reason, apart from any wording preferences, would be the number of language keywords.

Keywords are seperate from any library classes and such. I can create a method called DateTime without breaking or overlapping with the DateTime type, since I can just use namespaces to specify which one I want. But you cannot* create a method or variable called "public" because it is a language keyword.

Because of this most languages seek to keep the number of keywords to a minimum (some even boast how few keywords are used). Every word of the English language that is used as a keyword becomes unavailable to a programmer for other uses. When possible it's better to reuse existing keywords, rather then permanently remove another page from the programmers dictionary (especially if the language is already used, in which case a new keyword will break any code that used it as an identifier)

Ruhr answered 10/3, 2011 at 14:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.