none
About using directive RRS feed

  • Question

  • It is an entry level question but I am unable to find the answer in MSDN.

    using System;
    using System.Text;

    Since System is the highest level, it should cover System.Text as well, why we should also import System.Text namespace. It is a lower level under System. I think that using.System includes everything in this namespace.

    Friday, September 20, 2013 2:58 PM

Answers

  • The using statement brings the types defined in that namespace into scope.  Child namespaces are not included in that.  Here's how it basically works at the language level when you reference an identifier.

    1. The compiler looks in the current scope for an identifier with the same name.  The current scope is generally the method body in which you're contained.  It would find a match if the identifier was a local variable.
    2. If no identifier is found then it moves to the parent scope.  If the identifier is found then it uses it otherwise it keeps moving back until it gets to the global scope.
    3. If the identifier is not found in any scope then you get a compiler error.

    In C#, as with most languages, scopes can be nested.  The following start a new scope.

    1. File - identifiers at this level are in the global scope
    2. namespace statement
    3. type statement
    4. method statement
    5. block statement

    When you use the using statement (in the context of namespaces) you're bringing the identifiers defined at (2) into scope for purposes of name resolution.  Going back to your example you can imagine the scoping rules being logically like this:

    namespace System
    {
       namespace Text
       { }
    }

    Now when you use System you can see everything in it including the namespace Text but you still cannot see the identifiers in Text unless you add a using for that namespace as well.  However you can use a partial namespace reference such as Text.StringBuilder.  When the compiler sees Text it will eventually find it in the System namespace and then it will look for StringBuilder in System.Text.  This is actually useful syntax but in general you'll simply add a using statement to avoid the extra typing. 

    Using the partial reference can be problematic.  If you use Text.StringBuilder and Text is an identifier in one of the scopes prior to getting to the namespace System then you'll run into a conflict.  For example if you declared a member in a type called Text and then used Text.StringBuilder the compiler would find Text as a member and then look for StringBuilder on the type rather than as a namespace. 

    Personally I recommend that you use a using statement for the namespace you need common access to in a file (right click and select Resolve to make this easier), aliases for namespaces that conflict with each other and full namespace names for uncommon/single use references.  That's just my approach though.

    Michael Taylor
    http://msmvps.com/blogs/p3net

    • Marked as answer by ardmore Friday, September 20, 2013 4:46 PM
    Friday, September 20, 2013 3:29 PM
    Moderator

All replies

  • using keyword imports only types contained in the given namespace,not import nested namespaces

    please refer

    http://stackoverflow.com/questions/6297279/nested-namespaces


    Happy Coding, RDRaja


    Friday, September 20, 2013 3:16 PM
  • The using statement brings the types defined in that namespace into scope.  Child namespaces are not included in that.  Here's how it basically works at the language level when you reference an identifier.

    1. The compiler looks in the current scope for an identifier with the same name.  The current scope is generally the method body in which you're contained.  It would find a match if the identifier was a local variable.
    2. If no identifier is found then it moves to the parent scope.  If the identifier is found then it uses it otherwise it keeps moving back until it gets to the global scope.
    3. If the identifier is not found in any scope then you get a compiler error.

    In C#, as with most languages, scopes can be nested.  The following start a new scope.

    1. File - identifiers at this level are in the global scope
    2. namespace statement
    3. type statement
    4. method statement
    5. block statement

    When you use the using statement (in the context of namespaces) you're bringing the identifiers defined at (2) into scope for purposes of name resolution.  Going back to your example you can imagine the scoping rules being logically like this:

    namespace System
    {
       namespace Text
       { }
    }

    Now when you use System you can see everything in it including the namespace Text but you still cannot see the identifiers in Text unless you add a using for that namespace as well.  However you can use a partial namespace reference such as Text.StringBuilder.  When the compiler sees Text it will eventually find it in the System namespace and then it will look for StringBuilder in System.Text.  This is actually useful syntax but in general you'll simply add a using statement to avoid the extra typing. 

    Using the partial reference can be problematic.  If you use Text.StringBuilder and Text is an identifier in one of the scopes prior to getting to the namespace System then you'll run into a conflict.  For example if you declared a member in a type called Text and then used Text.StringBuilder the compiler would find Text as a member and then look for StringBuilder on the type rather than as a namespace. 

    Personally I recommend that you use a using statement for the namespace you need common access to in a file (right click and select Resolve to make this easier), aliases for namespaces that conflict with each other and full namespace names for uncommon/single use references.  That's just my approach though.

    Michael Taylor
    http://msmvps.com/blogs/p3net

    • Marked as answer by ardmore Friday, September 20, 2013 4:46 PM
    Friday, September 20, 2013 3:29 PM
    Moderator