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

Accessing USB devices from Virtualbox Windows guest on Linux host

This is one issue that had troubled me for long but was not important enough for me to put any effort into soving it. All this changed when I had to upgrade my BIOS on my Dell Optiplex 9020 machine. The problems are:

  • One cannot upgrade BIOS from a Linux machine, a Windows installation is a must. At least, a DOS bootable CD, USB Flash drive is needed
  • I run Manjaro on this machine and installing Windows just for the BIOS upgrade is just too much

So, I had to find a way to access USB devices on my Windows VM guest. I have never been able to do that. Primarily because, I never tried. This time I had to. So here is what is needed:

  1. Go to the Virtualbox Downloads page
  2. If the Virtualbox installed on the system is not the latest move to the Virtualbox Old Builds section
  3. Download the Extension PackVirtualboxWebsite_ExtensionPackLink
  4. Hit the link and the Firefox Save File Dialog pops up with Virtualbox selected as the app to open the file with. Just say OK:Firefox_SaveFileDialog_VirtualboxExtensionPack
  5. Virtualbox will try to install the Extension Pack. It will pop up the following EULA. Scroll to the bottom and the I Agree and I Disagree buttons will get enabled. You know what to choose:Virtualbox_ExtensionPack_EULA
  6. If all goes well, Virtualbox will throw this at you:Virtualbox_ExtensionPack_InstalledSuccessfully
  7. It is time for some radical changes in the system. Bring to life a terminal emulator and feed it the following command to add the desired user to the vboxusers group:
     usermod -a G vboxusers <User_Name>
  8. Now move to the settings of the Virtualbox Windows guest and click the USB settings. Make sure that you click on all checkboxes and finally the settings dialog looks like this:Virtualbox_USBSettings_AddFilter
  9. Make sure that the device that you want to access in the Windows guest is NOT mounted on the Linux host. If it is, unmount it. Then, click on the button with the USB icon which has the green colored plus (+) sign. The above picture shows the tooltip for the said button. Click on that and all available USB devices on the machine will be listed. Choose the one you want to add a filter for. Adding a filter will make the device available on the Windows guest. This makes the device completely inaccessible on the host. Every time you bootstrap the Windows guest, the filter will make sure that the device is available in the guest.
  10. Some driver installations will happen in the guest once the filter is added. A restart of the guest and host would be good for the society.