using KEYWORD IN C#
There are two ways of using the using keyword in C#:
- As a statement
- 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.
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:
- Remove Unused Usings
- Sort Usings
- Remove and Sort
Remove Unused Usings:
This option simply removes unused using directives. The end result would be like:
This option only sorts the using directives alphabetically. It does nothing else. Also, the sort can not be done in reverse alphabetical order:
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:
This keeps the class file clean and some unnecessary lines of code can be reduced (if that sort of thing appeals to you).
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:
- Go to the Virtualbox Downloads page
- If the Virtualbox installed on the system is not the latest move to the Virtualbox Old Builds section
- Download the Extension Pack
- 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:
- 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:
- If all goes well, Virtualbox will throw this at you:
- 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>
- 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:
- 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.
- 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.