Skip to main content

Optimize Namespace References

  • 2 minutes to read

Removes unused namespace references in VB, C# and XAML.

#Availability

Available from the context menu or via shortcuts:

  • when the caret is on a using or Imports statement.

#Options

You can specify the refactoring options via the Editor|Refactoring|Optimize Namespace References option page.

The option page includes the following options.

  • Sort namespace references - specifies namespaces sorting order. Available values are: Don’t sort, Alphabetically and By length.
  • Keep System namespace references on top - specifies whether or not System namespace references are always on top. The option is available only if namespaces sorting is on.
  • Never remove the following references - if the option is on, the refactoring does not delete namespace references listed below the option.

#Example

using System;
using System.Drawing;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MyApplication
{
    class TestClass
    {
        private Rectangle MyRect;
        public Rectangle GetRect(int x,int h)
        {
            MyRect = new Rectangle(x, x, h * 2, h);
            return MyRect;
        }
    }
}
Imports System
Imports System.Drawing
Imports System.Collections.Generic
Imports System.Text

Namespace MyApplication
    Class TestClass
        Private MyRect As Rectangle
        Public Function GetRect(ByVal x As Integer, ByVal h As Integer) As Rectangle
            MyRect = New Rectangle(x, x, h * 2, h)
            Return MyRect
        End Function
    End Class
End Namespace

Result:

using System;
using System.Drawing;

namespace MyApplication
{
    class TestClass
    {
        private Rectangle MyRect;
        public Rectangle GetRect(int x,int h)
        {
            MyRect = new Rectangle(x, x, h * 2, h);
            return MyRect;
        }
    }
}
Imports System
Imports System.Drawing

Namespace MyApplication
    Class TestClass
        Private MyRect As Rectangle
        Public Function GetRect(ByVal x As Integer, ByVal h As Integer) As Rectangle
            MyRect = New Rectangle(x, x, h * 2, h)
            Return MyRect
        End Function
    End Class
End Namespace

#Screenshot

rsOptimizeNamespaceReferences