10

The using directive gives us access to types without using their fully-qualified names:

using MyNamespace.Foo.Bar;

The using statement acts as syntactic sugar for try/finally blocks which ensure that objects are disposed of properly:

using(var x = new MyDisposableClass()) { ... }

These two situations seem to have nothing to do with one another. Why do they use the same keyword?

I doubt anyone would ever get the two cases confused, but it seems odd to not create a new word for a new piece of functionality.


As an aside, I've read about the double use of class in C++ as well (one for declaring classes, one for defining template parameters), but the second case eventually got its own keyword, typename, which makes more sense to me.

7

You are right in that both uses of using are unrelated.

However, there are several good reasons to use using in both cases.

  • The using statement was created for .NET 1.0 and makes semantic sense ("I'm using this namespace in the following code")
  • In both cases, using makes semantic sense. In plain English, you are using a namespace or you are using the declared disposable object.
  • By overloading the using keyword, the language designers did not have to create another reserved word in the language. This helps keep the language cleaner.

It is possible that given their time again, the language designers may have chosen to use import for namespace declarations to disambiguate the two choices (using a disposable object came in a later version of .NET). We will never know and it really isn't important since it's extremely clear which of the two meanings you're using due to the context.

Having said all of that, don't go and litter your code with using MyNamespace.DisposableObject = DisposableObject; namespace aliases everywhere.

  • Thanks. Can you expand on what you mean by "helps keep the language cleaner"? In my mind, overloading the keyword makes the language less clean. I'd think it would be cleaner to make a new keyword and use separate keywords for separate functionalities. – eigenchris Dec 9 '16 at 7:36
  • 5
    Keywords cannot be used as identifiers. Introducing a new keyword breaks each and every program that happens to use that keyword as an identifier. You can only introduce a keyword in a backwards-compatible manner if you have access to every line of source code ever written in the entire history of C♯, including proprietary source code, secret source code, military source code, and so on. – Jörg W Mittag Dec 9 '16 at 7:41
  • @JörgWMittag, Not completely true. C# has a whole bunch of contextual keywords that were added in ways that meant existing identifiers with that name don't conflict with the code. var being the classic example where an in-scope type named var takes precedence over the keyword. – David Arno Dec 9 '16 at 9:03
  • @JörgWMittag Or the alternative is to make the language version of the compiler selectable (like g++ --std=c++11, or to allow source files to specify a dialect (as in Haskell (?), Racket, Perl, even Python). A language can also be designed in a way that keywords never clash syntactically with possible identifiers, without reserving some identifiers (though that might be difficult for your run-off-the-mill language implementation using a LALR parser with a lexer in front). – amon Dec 9 '16 at 9:22
  • 1
    @eigenchris you can always preceden keword with a @ to use it as an identifier like @using – Thomas Schremser Dec 10 '16 at 12:16
4

Language designers generally try to limit the number of keywords. When a keyword is reserved it cannot be used as an identifier in user code, which is annoying if it is just the word you need. It is also a hindrance when porting code from other languages - and Microsoft wanted people to port code from Java to C#. C# does have more keywords than Java, but each new keyword would make it more difficult. Furthermore, the .net languages is designed to interact with API's written in different languages which may not have the same keywords, which leads to annoyances if a public member is also a keyword.

The two uses of using are unambiguous, since they appear in positions in the syntax, an "using" is an appropriate term in both cases. So it makes sense to reuse the same keyword. You will note other examples of re-use of keywords, for example:

  • new in generic constraints
  • in in foreach and in variant type parameters
  • defaultas a clause in switch and as an operator (default(T)).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.