Code Snippets for Parallel.For and Parallel.ForEach

Code Snippets for Parallel.For and Parallel.ForEach

Rate This
  • Comments 9

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" ?>
<CodeSnippets  xmlns="">
    <CodeSnippet Format="1.0.0">
            <Description>Code snippet for 'Parallel.For' loop</Description>
            <Author>Microsoft Corporation</Author>
            <Code Language="csharp"><![CDATA[Parallel.For($from$, $to$, $index$ =>
            $selected$ $end$

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?


Leave a Comment
  • Please add 3 and 7 and type the answer here:
  • Post
  • Sweet!

  • Oh, I forgot. Maybe one or two for using the TPL.

  • PingBack from

  • Sadly, snippets were a feature of 2005 that I never got around to using (the default ones didn't format like I do, and I didn't bother to learn the format/deployment process). This might change my mind, though.

    Now, if I ever see "Concurrency Rules/Suggestions for FxCop/Code Analysis", you folks will make my year! (yeah, yeah, it's a pipe dream, but the abstractions .Net is starting to provide are opening things up to some very interesting analyses)

  • Thanks, Keith.  We're certainly interested in static analysis rules/tooling for concurrency.  If there are any particular patterns you'd be interested in seeing rules for, please do let us know.

  • I'll put more detailed ideas on the forums (or is Connect feedback better?), but things that come to mind off the top of my head are:

    - wrap loops in Parallel./PLINQ if internal uses local memory/calls static methods

    - difficult heuristic, but use "PLINQ if possible for enhancement-by-abstraction" if some lower-level PFX is being used when PLINQ would do

    - any kind of means for detecting unnecessary use of shared memory when localized & threaded would be better

    - just about any generally-accepted algorithms for determining when some programming construct can be threaded

    And to be honest, I've never actually written a custom FxCop rule (the one rule I've wanted for years going back to C++ requires FxCop integraton with the XML dox), so I'm not sure the extent to which this stuff is even possible.

  • Thanks for the suggestions, Keith.  A forum discussion on the topic would be a great thing to start.

  • PingBack from

  • Thanks Stephen for providing the snippets.

Page 1 of 1 (9 items)