Namespaces

Could some one point me to a tutorial on how and more importantly when to use a namespace. My background is VB 6 so I am not familar with the concept.

Thanks.


Answer this question

Namespaces

  • barbarian

    fenris -

    If you are writing end-users applications, namespaces are pretty much up to your personal preference.  They allow you to organize your code and designate parts of your code that can easily be identified as grouped together.  If you are trying to build libraries, however, there are some good guidelines about how to best use namespaces, such as those at http://msdn.microsoft.com/library/default.asp url=/library/en-us/cpgenref/html/cpconnamespacenamingguidelines.asp.

    Thanks,
    Luke Hoban
    Visual C# IDE Program Manager


  • Dan Ward

    So, if I have an application that does file I/O, math calculations, Sorting etc. I could use namespaces to group all of my code into logical chunks instead of putting them into separate assemblies like I do in VB 6

    For example:

    fenris.FileIO
    fenris.Math
    fenris.Sorting

    What happens if I want to break some things down a little farther, say like this:

    fenris.Math.Numerical
    fenris.Math.Matrix
    fenris.Math.Vector

    Would each of these namespaces be separate logical chunks almost like separate dlls

  • dongjunming

     cgraus wrote:
    They are seperate chunks in the sense that if one namespace contains a class, another namespace cannot use that class without first referencing it explicitly by namespace.

    The .NET framework is the most obvious example of this.  Things like System.Text, System.IO, System.Imaging are all namespaces, and they divide up the framework into logical areas.  Some of these areas are in different dlls, some are not.  And one obvious example of how this helps is that there is a Thread.Timer, and a Windows.Timer, if you're using both namespaces, you need to be explicit about which one you're wanting to use.


    Thank you for your reply. It is starting to make more sense. I will try to put it into practice but it seems that namespaces would be really useful for large projects and smaller projects would simply be contained within one namespace.

  • David Bachy

    A namespace basically is a partition.  For example, the app I write is fairly large, and I use a different namespace for each discreet part of it.  I also had to do a lot of code to talk to a number of similar programs, so I create a namespace for the code to import from each program, so that within the namespace I can have names like DataImporter, instead of AvimarkDataImporter, etc. 

    Basically, by creating partitions, you limit which other code your code can 'see', it's as if they aren't in the project unless you scope them explicity, or put a using statement.  This creates discreet sections of code instead of one big application, which limits the possibilities when debugging, etc.


  • Werner de Jong

    They are seperate chunks in the sense that if one namespace contains a class, another namespace cannot use that class without first referencing it explicitly by namespace.

    The .NET framework is the most obvious example of this.  Things like System.Text, System.IO, System.Imaging are all namespaces, and they divide up the framework into logical areas.  Some of these areas are in different dlls, some are not.  And one obvious example of how this helps is that there is a Thread.Timer, and a Windows.Timer, if you're using both namespaces, you need to be explicit about which one you're wanting to use.


  • Namespaces