C# - using keyword

Most devs I work with are shocked to learn that the using keyword in the C# language is diatic. The obvious is for namespace inclusion but the less often seen use is for aliasing. For example, I hate having to reference the static members of ConfigurationSettings via the fully qualified name of System.Configuration.ConfigurationSettings. This is where aliasing can be cool; just give the class an alias with the using keyword like this:

  using cfg = System.Configuration.ConfigurationSettings;

This will allow you to refer to the members of System.Configuration.ConfigurationSettings using the cfg alias name.

5 Comments

  • Wow. Very cool tip.

  • What does 'diatic' mean?



    Before I displayed my ignorance, I checked with Merriam-Webster. The assistent editor who answered me thought you might mean 'dyadic'. It doesn't detract much from your point, but it bugs the bejeesus out of me not knowing.



    - Stan



    ------------------

    "You keep using that word. I do not think it means what you think it means." - Inigo Montoya

  • Diatic is a term often associated with the phrase Dual Use

  • Actually, using is triatic (now, what does that mean?). It can be used to to wrap a disposable (implements IDisposable) object in a scope so that the Dispose() method is called automatically when exiting the scope. It is similar to C++ destruction of automatic objects and is very cool when waiting for finalization is not an option and you don't want to remember calling dispose() all over the place.

  • NOTE: The scope of this keyword is only file scope.
    It is not *actual* replacement of the C typedef keyword.

Comments have been disabled for this content.