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?
using
statement and theusing
directive are two separate things, but yes, I would like to know why there isn't awith
oruse
statement, or perhaps aimport
oruses
directive instead – Deborausing
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. – Marticausing
, it's possible to define type aliases using theusing
directive. – Deborausing 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 oneusing
directive value and checks if it has found the correct existing type. – Marticawhere
(and alsovar
,select
,yield
orfrom
) are considered contextual keywords, they are not real reserved keywords likeusing
and can, for instance, be used as a variable name without using the @-sign, resulting in allowed, but silly code likevar var = 42
. – Particular