Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 980
G-Wiz Avatar asked May 06 '10 20:05

G-Wiz


People also ask

Why did they call it C?

After language 'B', Dennis Ritchie came up with another language which was based upon 'B'. As in alphabets B is followed by C and hence he called this language as 'C'.

Why was C originally created?

C was designed as a minimalist language to be used in writing operating systems for minicomputers, such as the DEC PDP 7, which had very limited memories compared with the mainframe computers of the period. The language was devised during 1969–73, alongside the early development of the UNIX operating system.

How old is the C language?

In 1978, Brian Kernighan and Dennis Ritchie published the first edition of The C Programming Language.

When did C come out?

In 1978, Brian Kernighan and Dennis Ritchie published The C Programming Language, which would serve as the language reference until a formal standard was adopted.


1 Answers

Your question assumes 3 contextual meanings to 1 keyword is more complex than 3 different keywords with different meanings. Some may contest this.

In my years of C# coding, I can't say I've ever been confused as to the meaning of a 'using'; it's always clear from the context.

like image 188
Judah Gabriel Himango Avatar answered Oct 24 '22 20:10

Judah Gabriel Himango