Saturday, 11 August 2018

c# - 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 HelloWebAPI.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;


public class ProductsController : ApiController
{}


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



using HelloWebAPI.Models;
using System;
using System.Collections.Generic;
using System.Linq;

using System.Net;
using System.Net.Http;
using System.Web.Http;

namespace HelloWebAPI.Controllers
{

public class ProductsController : ApiController
{}



My supervisor told me that it is okay and it is supposed to be okay, because it is an official MS tutorial on the subject.



** What I want to know that why this doesn't matter, so that I have a better understanding about it? **


Answer



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.


No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print ...