C# Distilled Code Snippet Library

Within Visual Studio 2005 the Visual C# team provides 37 code snippets that allow the C# programmer to accelerate writing several common code constructs such as constructors and properties. One of the more useful code snippets that is provided by the Visual C# team is the exception code snippet that simplifies the creation of custom exception types that inherit from the base System.Exception class. It would have been more useful however if the code snippet provided Xml comments for the methods defined within the newly created exception type, in addition to providing an override of the GetObjectData method and an associated deserialization constructor. Within this blog posting we will explore the creation of additional code snippets and show how they can be installed into Visual Studio 2005 using the Visual Studio Installer.

At the time of writing there are 53 code snippets within the C# Distilled code snippet library, most of which are merely an extension of one of the code snippets provided by the Visual C# team. An example of a code snippet that has been extended is the _ctor.snippet which provides a default Xml comment whose wording follows the standard format for constructors within the .NET Framework. In addition to the Xml comment, the constructor is also enclosed within a code region with the name of the region being the same as the signature of the constructor. Use of code regions in this way is required within the C# coding standards that I have used on several commercial projects, if you’d be interested in me publishing the C# coding standard here then please let me know and I’ll move it up the priority list for postings I have in mind for the next few months.

Those of you with keen eyes will have noticed that the extended code snippet has the same name as that supplied by the Visual C# team aside from the preceding underscore, this is such that I have install the extended code snippets while keeping those provided by the Visual C# team.

Instead of listing all 53 code snippets within the C# Distilled code snippet library, and how they might differ from those already included within Visual Studio 2005, I have instead provided a Microsoft Excel spreadsheet that lists the code snippets and provides a brief description along with the Visual C# code snippet it is derived from (where appropriate). The spreadsheet is included in the zip file that can be downloaded below.

To install the C# Distilled code snippet library download the .zip file which is linked above. Open the .zip file and expand the files to a location of your choosing, then double click on the .vsi file to begin the installation. Once the installer opens you will be presented with a dialog such as the following and you should simply click the Next button to proceed.

The next screen you will be presented with will ask you if you wish to proceed to install the components within the .vsi file because the file is not digitally signed, there is no harmful code within the .vsi file however you can verify the .vsi files contents by opening it with WinZip as that is really all the .vsi file is. The Visual Studio Installer file is a .zip file renamed to .vsi, containing a .vscontent file that tells the Visual Studio Installer runtime which files it should install. These files are also included within the .vsi file.

The following screen asks that you select the code snippets on the left of the window that you would like to install, and then on the right of the window you choose the appropriate installation location. To select all the code snippets click on the first code snippet and then press Ctrl+A to select all the code snippets and then click the installation location, either the Visual C# folder (where the Visual C# team installs their code snippets) or the My Code Snippets folder.

Once you have selected the code snippets you would like to install you simply click next and then you are asked once again to confirm the installation by clicking the Finish button on the following screen.

Upon completion of the installation you will recieve an update telling you that the installation of each code snippet was successful. If an error occurred while copying the file you’ll also see the errors within the following screen.

Now that you have the code snippets installed you can look at the differences between some of the extended code snippets and their counterparts developed by the Visual C# team, in a follow up blog posting I’ll introduce the code snippet functions that enable the code snippets to determine the name of the class within which the code snippet is being executed or if the appropriate using statement is present such that classes are referenced appropriately using the fully qualified name or the simple type name.

Leave a Reply

Your email address will not be published. Required fields are marked *