The USING keyword in C#: How to clean unnecessary using directives in Visual Studio

using KEYWORD IN C#

There are two ways of using the using keyword in C#:

  1. As a statement
  2. As a directive

using KEYWORD AS A STATEMENT

MSDN says the using keyword

Provides a convenient syntax that ensures the correct use of IDisposable objects.

This is also a way of saying that this is the standard way of releasing unmanaged resources. The following is an example of its usage:


using(StreamWriter grepLog = new StreamWriter(fileName, true))
{
// ...log code here
}

using KEYWORD AS A DIRECTIVE

This is the most common usage of the using keyword. The following example shows how using keyword is used to access types in a namespace. This practice of using the using directives to access types in a namespace allows the developer to avoid fully qualifying the type. This save a lot of time and typing.


using System;

using System.Text;

Another use of using is to create aliases. An excellent discussion on this topic can be found in this Stackoverflow thread.

CLEANING using DIRECTIVES

There are many reasons why your .cs file may end up having more using directives than necessary. There is a very quick way to clean and organize using directives in Visual Studio. Just right-click on the text editor. The context menu pops up and the second option in the menu is Organize Usings.

This menu item has three sub-items:

  1. Remove Unused Usings
  2. Sort Usings
  3. Remove and Sort

OrganizeUsingsInVisualStudioTextEditorContextMenu

Remove Unused Usings:

This option simply removes unused using directives. The end result would be like:

OrganizeUsingsInVisualStudio_RemoveUnusedUsings

Sort Usings:

This option only sorts the using directives alphabetically. It does nothing else. Also, the sort can not be done in reverse alphabetical order:

OrganizeUsingsInVisualStudio_SortUsings

Remove and Sort Usings:

This option removes all unused using directives and sorts the list too. This sorting is also done alphabetically and no reverse alphabetical sorting option is available:

OrganizeUsingsInVisualStudio_RemoveAndSortUsings

This keeps the class file clean and some unnecessary lines of code can be reduced (if that sort of thing appeals to you).

Advertisements

3 comments

  1. I am so glad you posted that question on programmrs, it is honestly so fucking annoying that half the time when I Google a question, I just see all the answers as people saying “This belongs on the this and that other stack board” and no solution. Literally half of the people on the site probably just sit there looking for off topic questions and telling people to ask their question somewhere else. Not helpful.

    Like

    • Chelsea, do not bother about those retards. There are many great resources on the internet for learning.

      It is really annoying that these people think all those who post questions are stupid and incompetent. I post question not because I am stupid or incompetent. I have spent a lot of time learning concepts on my own. It is not easy but I love doing it. A correct pointer can help me be more productive.

      I post because I know there are many guys out there who need the right push in the right direction. A good healthy discussion can bring out subtle points. Everyone has a different perspective and different experience. So a discussion on forum can slowly become a database of collective intelligence. This is what can help a newcomer to learn in a wholesome way.

      They tried to make me feel stupid for asking a question. That guy Doval who ridiculed me is a recent graduate. He needs training in team work. He would be a liability for any team.

      Like


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s