none
namespaces and using-alias-declarations RRS feed

  • Question

  • Hi all,

    I've been having a discussion with our team's solutions architect about the using-alias-declaration statement and the position of them within a class file and I've been having community discussions around this topic trying to find out what the best practise amongst developers is and if any performance penalties imply.

    As a developer, I've come to find a form of coding to my liking, and using this in conjunction with our company's coding best practises have come to enjoy a readable coding style, but this has recently come under fire by my new solutions architect and I would like like to find out why. If we look at the following two pieces of code, you'll notice that they look exactly the same, except for the placement of the using-alias-declaration:

    Class 1:
    using System;  
     
    namespace MSDNForumQuestion  
    {  
       public class AskQuestion  
       {  
       }  

    Class 2:
    namespace MSDNForumQuestion  
    {  
       using System;  
     
       public class AskQuestion  
       {  
       }  

    In various discussions I've had with .Net specialists in our community two very important aspects was raised:

    1.) having the using-statement within the namespace declaration is seen as coding best practises as highlighted by source analysis tool, StyleCop.
    2.) there is some minor performance penalties with the using statement as the assemblies that are referenced will only be loaded when a method within your class file, requires a method or prop from that 
         referenced assembly

    What according to CLR complaincy is the correct way of specifying using-alias-declarations or is this a team-specified best practise. Is there performance issues involved with loading assemblies at run time and what impact does this have on larger application, I mean, with in a web application (ideally a little micro web application) there should be no performance penalties, if the application makes use of only the required assemblies (System.Web, 3rd party controls) as en example.

    Many developers start off with the using statements, reference their assemblies (at that point nothing is actually referenced in memory) and then start namespacing, while others prefer adding the using-statements within the namespace block. There is not a lot of information about this on the interwebz, but I did find that the ECMA standards document for C# highlights that the using-statements should be added within the namespace-block for various reasons, over having them outside the namespace-block.

    Regards,

    Eric
    Friday, July 4, 2008 5:34 AM

Answers

  • > there is some minor performance penalties

    At run time, there will be no performance difference.  The using statements only affect how you must qualify type names in your code.  In other words, they are a convenience so that you do not have to specify the fully qualified type names in your code.  In the compiled (MSIL) code, all types are fully qualified, regardless of how you are making use of using statements.

    Friday, July 4, 2008 3:51 PM

All replies

  • > there is some minor performance penalties

    At run time, there will be no performance difference.  The using statements only affect how you must qualify type names in your code.  In other words, they are a convenience so that you do not have to specify the fully qualified type names in your code.  In the compiled (MSIL) code, all types are fully qualified, regardless of how you are making use of using statements.

    Friday, July 4, 2008 3:51 PM
  • Hi,

    That makes sense, so regardless of whether you specify the using-alias within or outside the namespace block, its only purpose is to make the calling of qualified namespaces easier to access?

    Regards,

    Eric
    Sunday, July 6, 2008 1:14 PM