Click here to Skip to main content
15,889,552 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to understand how using statement inside namespace is better.
#Using statements inside namespace

C#
namespace Demo_Console.Account
{
    using System;

    public class Math
    {
        public static string Min(int a, int b)
        {
            return "Hello I am custom";
        }

        
    }
}



#Using statements outside namespace
C#
using System;
namespace Demo_Console.Account
{
    

    public class Math
    {
        public static string Min(int a, int b)
        {
            return "Hello I am custom";
        }

        public static string Print()
        {
            return (Math.Min(10, 10));
        }
    }
}




How does this make difference??

What I have tried:

using statements inside or outside namespace
Posted
Updated 2-Mar-17 23:13pm

 
Share this answer
 
Comments
Afzaal Ahmad Zeeshan 3-Mar-17 5:15am    
5ed.

Also see Solution 2, as I provide some indepths about namespaces themselves.
Karthik_Mahalingam 3-Mar-17 5:17am    
Thank you AAZ
Apart from what was mentioned in Solution 1, there is significantly no (or very less) difference in performance for this change as well. Because, one way or the other, compiler takes care of "when" and "how" after compilation, because of optimization. The concept of namespace span back to the days of C++, where the namespaces in C were quite "complex", and you would have a namespace of struct, or union etc. C++ introduced the namespaces, that can be created on demand. C# uses the same concept, and allows you to map the types to a particular namespace, or find the objects within.

Likewise, in C#, the types are first checked up locally, and then any namespaces are checked to include the type. So, a namespace within the namespace (or class) would only mean that this package reference is only available within the scope of the class. The other classes, or namespaces within the same file do not reference it, and thus they would use the types provided by programmer; see the example on the SO thread that Solution 1 provided.

Scott Hanselman explained this pretty much neatly in his blog post, Back to Basics - Do namespace using directives affect Assembly Loading? - Scott Hanselman[^]
 
Share this answer
 
Comments
Karthik_Mahalingam 3-Mar-17 5:20am    
5!
liked from the blog,

Don't believe everything you read, even on a Microsoft Blog.
Don't believe this blog, either!
Decide for yourself with experiments if you need a tiebreaker!
Afzaal Ahmad Zeeshan 3-Mar-17 5:26am    
Thank you, Karthik!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900