Introduced in Visual Studio 2005, Code Snippets allow you to quickly insert reusable blocks of code into your project. For example, if you want to quickly write a for loop in C#, you can simply type “for” into your code file, and IntelliSense shows you the “for” code snippet:
Now you press the tab key twice, and Visual Studio automatically expands this for you into a for loop, allowing you to quickly replace things like the iteration variable’s name:
One of the coolest things about Code Snippets, however, is that you can write your own. Imagine being able to type “parall” and have a Code Snippet for Parallel.For pop up:
When you expand this, you’d get a Parallel.For loop:
“Magic,” you say? Not quite. All you have to do is write a custom snippet that looks as follows:
<?xml version=”1.0″ encoding=”utf-8″ ?>
<Description>Code snippet for ‘Parallel.For’ loop</Description>
<Code Language=”csharp”><![CDATA[Parallel.For($from$, $to$, $index$ =>
Save that to a file parallelfor.snippet in a directory of your choosing. In Visual Studio, go to Tools | Code Snippet Manager…, add the directory into which you stored the snippet, and you’ll be off and running.
The parallelfor snippet file as well as another example for parallelforeach are attached to this blog post. Are there any other useful snippets you’d be interested in having related to parallel programming in general or specifically to Parallel Extensions?