Renaming usings in C#

A collegue showed me something useful today: renaming namespaces/using statements in C# which may result in cleaner code.
Some simple code to demonstrate this:

using Test = System.Diagnostics;

namespace ConsoleApplication1
{
  class Program
  {
    static void Main(string[] args)
    {
      Test.Debug.WriteLine("Testing new using");
    }
  }
}

 

Happy coding ;)

Remove and sort using statements

A lot of people don’t know this but you can actually clean up your using statements in your Visual Studio projects almost automatically.

In case you didn’t know, here is how to do it:

You have a big list of using statements in your class. It might look like this by default:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

Just do a right-mouse click on the using statements and select:
[Organize Usings] > [Remove and Sort].

This will remove all the unnecessary and unused usings. The result might look as clean as this:

using System.Windows.Controls;

Sometimes, nothing could be removed or sorted but in most new class files there are a lot of default usings. It might be good to clean them once in a while, especially if you wrote and removed lots of (test) code in a class.

Happy programming ;)