locked
namespace RRS feed

  • Question

  • recently i have observed using statements are written with in the namespace as below:

    namespace Test{
        using global::System;
        using global::System.Collections.Generic;
        using global::System.Linq;
        using global::System.Text;
        ....
    }

     what is the advantage do we get if we follow this way, other than the conventional way which we follow regularly?

    Thursday, August 5, 2010 6:31 AM

Answers

All replies

  • Global is a default namespace for any c# program. the above declaration is helpful when you have your own custom classes in matching namespaces see the link below for details description

    http://msdn.microsoft.com/en-us/library/c3ay4x3d.aspx


    Manish Sati
    Thursday, August 5, 2010 6:49 AM
  • this is just for avoiding naming conflicts.It provides the ability to access a member in the global namespace is useful when the member might be hidden by another entity of the same name.When the left identifier is global, the search for the right identifier starts at the global namespace.

    Further reading http://msdn.microsoft.com/en-us/library/c3ay4x3d.aspx

     

    Thursday, August 5, 2010 6:51 AM
  • Everyone has focused on the "global::" part of the code and missed the text of the question: "written within the namespace".

    That's exactly the same as when you declare a variable inside a block: it's only visible from inside the block.

    If you have only one namespace block in your file, writing the using statements inside or outside the namespace block doesn't change anything.

    The following, however, can be useful. Try to figure out what it does and ask if you don't understand.

    namespace MyNameSpace
    {
     using System.Timers;
    
     class MyClass1
     {
      public Timer t;
     }
    }
    namespace MyNameSpace
    {
     using System.Windows.Forms;
    
     class MyClass2
     {
      public Timer t;
     }
    }
    namespace MyNameSpace
    {
     using System.Threading;
    
     class MyClass3
     {
      public Timer t;
     }
    }
    
    Thursday, August 5, 2010 10:06 AM