Why did the C# designers attach three different meanings to the 'using' keyword?

Posted by gWiz on Stack Overflow See other posts from Stack Overflow or by gWiz
Published on 2010-05-06T20:30:53Z Indexed on 2010/05/06 20:38 UTC
Read the original article Hit count: 179

Filed under:
|

The using keyword has three disparate meanings:

  1. type/namespace aliasing
  2. namespace import
  3. syntactic sugar for ensuring Dispose is called

The documentation calls the first two definitions directives (which I'm guessing means they are preprocessing in nature), while the last is a statement.

Regardless of the fact that they are distinguished by their syntaxes, why would the language developers complicate the semantics of the keyword by attaching three different meanings to it? For example, (disclaimer: off the top of my head, there may certainly be better examples) why not add keywords like alias and import? Technical, theoretical, or historical reasons? Keyword quota? ;-)

Contrived sample:

import System.Timers;
alias LiteTimer=System.Threading.Timer;
alias WinForms=System.Windows.Forms;

public class Sample {
  public void Action {
    var elapsed = false;
    using(var t = new LiteTimer.Timer(_ => elapsed = true) {
      while (!elapsed) CallSomeFinickyApi();
    }
  }
}

"Using" is such a vague word.

© Stack Overflow or respective owner

Related posts about c#

Related posts about language-design