C# Namespace Ordering – Coding Convention Guide

asp.net-corecnamespace

Is there a general coding convention in ordering namespaces? Is it always order of importance, or alphabetical? Currently I order by main important ones first. I know it does not affect program, just curious about coding convention.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.EntityFrameworkCore;

Best Answer

How would you determine the importance? The simplest way of ordering the usings is the to apply an alphabetical order; However, it is often useful the make the difference between usings pointing to the .NET Library and your own namespaces:

using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

using MyCompany.MySolution.MyModule1;
using MyCompany.MySolution.MyModule2;
using MyCompany.MySolution.MyModule3;

Within these two groups I would still apply an alphabetical order. I keep my own namespaces below the system namespaces, because like this, they are closer to the rest of my code and more likely to be visible.

Keep it simple!