Écrivez-moi
antibuginformatique@yahoo.ca

english

 Accueil   Services   Téléchargements   Articles 

Uses of using


Patrick Lavoie
anti-bug informatique
December 1st 2004
Copyright © 2004


Did you know what the using keyword could be used for?

  • It can be used to dispose of objects implementing the IDisposable interface when they go out of the using block.
  • It permits the use of types within a namespace so you don't have to fully qualify the objects.
  • It can also be used to alias classes or namespaces so you can avoid name collisions. I guess that calling it alias would have been unintuitive!

  • If we think about it, the second and third usages are in fact both aliases. The difference with the second case is that you simply don't give any name to the namespace. You just create an unnamed alias.

    So if you don't like the class name Object and you would prefer to call it (let's be imaginative and create an alias that no one could have ever thought of) object (without the capital "O"), we could do something like:

    using object = System.Object;

    Then, whenever we want to use the Object class, we can either call it System.Object, Object or object.





    Copyright © 1996-2017
    anti-bug informatique inc.
    Tous droits réservés.