xofz
xofz

Reputation: 5660

Microsoft coding style question

I had to figure out a way to ask this that wasn't subjective, so this is specifically for Microsoft's coding style. In the ASP.NET MVC source, code files look like this:

// Copyright info

namespace System.Web.Mvc {
    using System;

    // blah blah
    // ...
}

Note that 'using System' lines up nicely with the namespace. If I was to apply this style to my company's code, should I put 'using' statements for my company's namespaces directly below as well (so that it lines up)? When I put 'using' declarations at the top, I usually start with .NET namespaces first, so that's why I'm unsure. For example, should I do this:

namespace MyCompany.MyProduct.Something {
    using System;
    using MyCompany.MyProduct.SomethingElse;
}

or this:

namespace MyCompany.MyProduct.Something {
    using MyCompany.MyProduct.SomethingElse;
    using System; 
}

I'm tempted toward the latter.

Upvotes: 1

Views: 571

Answers (4)

Brad Bruce
Brad Bruce

Reputation: 7807

You've got two things going on here.

  1. Alignment - They aren't lining up anything. They're using 4 space indents (the default)
  2. Order of using statements - Typically you will find the system namespaces first. Under that you will find a hierarchy of levels:

    using System;  
    using System.Collections;  
    using System.Collections.Specialized;
    

Upvotes: 0

Partha Choudhury
Partha Choudhury

Reputation: 534

The convention I follow is:

  • System namespaces
  • Microsoft namespeces
  • Any external / third party namespaces
  • Company's internal namesapces - common / core
  • Company internal namespaces - local / project

Starting with wider scope towards narrower scope...

Upvotes: 0

Adrian Godong
Adrian Godong

Reputation: 8911

Microsoft StyleCop dictates using System.* first then your custom library namespace (i.e. the first option).

Upvotes: 1

Reed Copsey
Reed Copsey

Reputation: 564373

There is no single Microsoft style, although there have been attempts to consolidate their standardizations.

That being said, StyleCop forces all System namespaces to be listed first...

Upvotes: 7

Related Questions