Search code examples
c#coding-stylenamespaces

Place usings inside or outside of namespace


I was learning MVC WebAPI and I was following a tutorial and everything was going fine untill I saw the following:

namespace HelloWebAPI.Controllers
{
    using System;
    
    public class ProductsController : ApiController
    {
        
    }
}

What we usually do is that we add the resources\scope in the beginning like this:

using System;

namespace HelloWebAPI.Controllers
{
    public class ProductsController : ApiController
    {
        
    }
}

I want to have a better understanding about it

Thanks.


Solution

  • There is a difference, small but there is.

    It's all about the sequence of name resolution made by compiler. The good answer on subject you can find here:

    Should Usings be inside or outside the namespace

    In practise in first case compiler, in case could not find a type information immediately, would search among namespaces declared inside using. In second case, instead, would search first among the actual namespace and after only go to search inside declared outside.