Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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?

like image 564
David Hoerster Avatar asked Mar 10 '11 14:03

David Hoerster


People also ask

Why do we use %d in C?

In C programming language, %d and %i are format specifiers as where %d specifies the type of variable as decimal and %i specifies the type as integer. In usage terms, there is no difference in printf() function output while printing a number using %d or %i but using scanf the difference occurs.

Why semicolon is used in C?

Role of Semicolon in C: Semicolons are end statements in C. The Semicolon tells that the current statement has been terminated and other statements following are new statements. Usage of Semicolon in C will remove ambiguity and confusion while looking at the code.

Why is C named so?

Quote from wikipedia: "A successor to the programming language B, C was originally developed at Bell Labs by Dennis Ritchie between 1972 and 1973 to construct utilities running on Unix." The creators want that everyone "see" his language. So he named it "C".

What is || in C programming?

Logical OR operator: || The logical OR operator ( || ) returns the boolean value true if either or both operands is true and returns false otherwise. The operands are implicitly converted to type bool before evaluation, and the result is of type bool .


1 Answers

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.

like image 186
Doctor Jones Avatar answered Oct 10 '22 04:10

Doctor Jones