Code snippets in Whidbey

One of the other hats I wear on the team, is looking after our samples story. We are looking at our story for code expansions right now.

A code expansion simply put is a file that you author with some code in it. You can add it to a folder or folders in a special place on the machine. When you are in the IDE you can invoke a command that lists these folders and lists the expansions you can pick from in an intellisense like view. In Whidbey we are thinking of providing some common tasks that users would use by default.

Of course the big question is which tasks? Right now we are thinking of organizing it by areas of the framework you would use - so you would hit a key combination and see a list like:

  • Collections
  • Data
  • Email
  • Files
  • ...

You could then pick one, say Files and you would see a list of tasks underneath it:

  • Reading from a file
  • Writing to a file

Selecting the task would paste the code in for you.

Sounds simple right? Until you hit the million dollar question - what subset should you provide? What are the simple tasks that a user does often enough that makes itself a task and satisfies a broad percentage of our customers needs? For e.g. reading and writing files is something I could see people doing often enough that it warrants adding - but having code that deals with WMI for e.g. might be something obscure for our customers.

So what do you guys think? What would you have as the hierarchy? What would you have as the tasks? Let me know your feedback...I'm thinking about this a lot right now so this a good time to influence me 🙂

Comments (24)

  1. This is obviously extensible, right? How does this work with normal templates (like typing for giving us a for loop)?

    I use CodeRush from DevExpress. For common things, like creating a new DB command to do a sproc, I assign "newcomm ". Every time I type newcomm<space>, it fills in and puts the cursor where I define in the template. Hopefully Whidbey will expose this for us to edit. Esp. since I don’t like the default formatting that the C# team uses.

    Apart from that, I think if there are 10-15 categories, that’s a good base. If I don’t know what WMI is, I won’t type it. The few times I do use WMI, it’s great to have snippets available quickly. If the categories and snippets are user-managable (with per-project option perhaps :)?), then it’s not a problem.

  2. David Nicholson says:

    It might also be good for things I don’t do often or don’t know so well. So not just common tasks.

    My personal frequent uses would include collections and files.

  3. John Cavnar-Johnson says:

    I think the more common tasks from the less common namespaces are the most important to have. Getting started with WMI, Perfmon, MSMQ, etc. is often the hardest part. Code snippets would be great for those areas.

  4. Scott Allen says:

    Collections, Data, and Files are good categories.

    A couple ideas:


    – Web request

    – Web request via POST

    – Web request with credentials


    – Load from stream

    – Save to stream

    – XSLT transformation

    – XPATH / XQuery search

  5. Louis Parks says:

    I’ll second Scott Allen’s ideas.

    A few others would be expansions for caching and iterating over collections.

    I think the most important thing is that expansions are *very* easy for us to add/modify. I don’t mean easy like enterprise templates (after reading for 30 mintues, you might have modified all the right files in all the right places). I mean easy like, add a file to a directory and away you go.

  6. Ferris Beuller says:

    Patterns like IDisposable etc would be good to have done for me.

    If I make a class and want start to code the IDisposable pattern, can I just say "hey, VS dude there, can u like ya know, read my mind and do this IDisposable thang for me S’all good". and it goes "yeah, sure dude, ding, done dangety".

  7. Ferris Beuller says:

    For Intellisense, how about fixing the listing so it catagorizes the actual things into like… classes, enums, events, etc etc Im tired of having to scroll through a list of alphabetical sorted thigns for all events and end up having to go to the object browser for a event list hunt.

    Would be niec is this was an option to catagorize intellisense popup boxes.

  8. I disagree with "getting started" code being placed in the common snippets. Snippets should be good, direct code, not a crutch for someone who’s learning something. I wouldn’t want the Reflection.Emit snippets (if there are any) to be littered with non-real-world "getting started" snippets. That’s what documentation samples and SDK samples are for.

  9. Shay,

    I think that with this particular issue, you will have a pretty difficult time. In my opinion, the main use that expansions will have is for code that organizations or individuals use frequently which is not offered by the general framework. Given that the audience of the .NET framework is so vast (compared to most, if not every other organization’s product), I would think that any expansions for commonly-performed tasks are really just functional points that are missing from the framework, and should be filled in.

    – Nick

  10. Adrian Moore says:

    Also, support the concept of an online folder where Microsoft can post new templates after the product is released.

  11. In the bag tonight: Less bitch’n and whin’n. Counts:Blogging: 8; Dev: 22; Otherwise: 8; SQL: 5; WILY: 8. Line of the night:

  12. Nick, so you’re saying that an expansion template that sets up (as a simple example, even though most data access components would already do something similar):

    SqlConnection connection = new SqlConnection(<cursor>);

    SqlCommand command = new SqlCommand();

    command.Connection = connection;

    command.CommandText = "<cursor>";

    using (connection) {



    Is just because the sql API is missing something? I see expansions as a way to write more code in less time by taking out repetitive, but needed, code. I use CodeRush and have many specific snippets setup, and it saves me a lot of work, where no better API could help.

  13. Rick Byers says:

    I’ve thought for a while that it takes too much work to create a new Exception type that follows all the guidelines. I’d like to see an expasion for new exception classes with all the correct constructors etc., perhaps even with some meaningfull XML comments.:


    public class MyException : System.ApplicationException


    public MyException()



    public MyException( string message )

    : base( message )



    public MyException( string message, System.Exception inner )

    : base( message, inner )



    public MyException(

    System.Runtime.Serialization.SerializationInfo info,

    System.Runtime.Serialization.StreamingContext context )

    : base( info, context )




  14. Steve Hiner says:

    I love this idea. It would only be fully useful if we could easily add new templates and if the format was standarized enough that people could post their snippets. I totally agree with Adrian that MS should provide updated templates on their site.

    A couple things that immediately jump to mind that I’d like to have snippets for:

    Serialize/Deserialize for XML and Binary, using streams or files

    Send email using system.web.mail

    Those are 2 of the things I most often find myself looking around in old code to copy and paste it.

  15. Steve Hiner says:

    You could include both Quick Start type code and commonly used code if you separated them in the menu.

    Perhaps the commonly used code could be expanded in the main intellisense popup but there could be a QuickStart entry at the bottom that would give access to snippets that aren’t realistic in production code but would give the user a boost with a new namespace. The quickstart could basically include the code we currently see as examples in the documentation.

  16. I used CodeSmart for this similar thing, then I just went back to using QuickCode for .NET, it is free if you dont register it. CodeRush looks like it has promise though.

    I know this is off topic now, but how did you get a serializable exception working? I always get the error "Cannot serialize class with properties decorated with declarative security attributes."

  17. Update (1/31/08): It looks like Snippy has now found a home on CodePlex . I haven’t been maintaining

Skip to main content