Visual Studio Toolbox: Creating and Using Code Snippets

Download this episode

Download Video

Description

In this episode, we explore code snippets, an underappreciated feature of Visual Studio.

Code snippets are existing pieces of code. All you have to do is type the snippets shortcut and Visual Studio writes the code for you. Watch as we first use a couple of snippets that ship with Visual Studio. Then, see how to use the Snippet Designer extension and create your own snippets. With this extension, you can highlight code, right-click, and Export as Snippet. Very cool!!

Embed

Format

Available formats for this video:

Actual format may change based on video formats available and browser capability.

    The Discussion

    • User profile image
      Gordon

      I'm disappointed that snippets only work for vb and c#.
      But that does explain why this feature isn't used so much.

      Microsoft owned languages gets "special" treatment yet again.

      Disgusting !

    • User profile image
      magicalclick

      thanks for the vid. I didn't use it because it was confusing. But, now it make sense.

    • User profile image
      magicalclick

      Question:

      Why does VS C# Express doesn't have the same amount of Extensions? Like your Snippet Designer is not in there. I have VS Pro, but, I prefer to use Express and I wasn't able to get the extention for Express. Any reason why? Thanks.

    • User profile image
      rogreen

      @magicalclick:The Express editions do support extensions, but not all extensions support the Express SKUs. The author of the Snippet Designer built the VSIX to install on Pro and above. I don't know if this is because it won't work as well in Express, but I will ask him.

      Robert

    • User profile image
      SheldonS

      Snippets can be a real time saver!  I've been using them for about year now, wish I had started using them earlier.

    • User profile image
      mmanela

      @magicalclick: I would love if I could have Snippet Designer support Express but the Express Sku's only support certain extensions which are specially signed by Microsoft. Even if I added Express to the list of editions in my VSIX it would still not run since Express won't let it. 

    • User profile image
      danfromisra​el2

      it's very hard to understand the code... quality is quite law

      any chance for high quality video?

       

      great video though !

      Thanks

    • User profile image
      rogreen

      @danfromisrael2: Are you watching the video in the browser? Did you download the High Quality WMV? The quality on that should be good.

      Robert

    • User profile image
      HugoAgueroR

      Great demonstration Robert.

      I'm a beginner in VS2010 and I can't make the ide to show the snippets directly with intellisense, How can you do that? Is a special command  needed to get that?

    • User profile image
      rogreen

      @HugoAgueroR:

      What version of Visual Studio do you have? Is it one of the Express products (VB, C#, Visual Web Developer)? Or is it Professional or Premium or Ultimate?

      And what type of project are you building and what type of file is open when you are trying to get snippets to work? Is it a vb or cs code file? An HTML page? An XML or XAML page?

      Robert

    • User profile image
      HugoAgueroR

      Thanks Robert

      I'm working with Visual Studio 2010 Ultimate. I'm using the snippets on a multilayer aplication that use WPF as a Presentation layer.  If I use the command Insert Snippet I can find my own snippets but It's not displayed directly on intellisense. The shorcut options works fine.

    • User profile image
      rogreen

      @HugoAgueroR:

      I apologize, but I am still not clear what isn't working. If you type ctrl+k+ctrl+x or if you right-click and select Insert Snippet or use the Insert Snippet command, you get a popup that shows the categories of snippets. Do all 3 of those work for you?

      If you type the snippet's shortcut and then press Tab twice, VS adds the code. Is that working for you?

      Robert

    • User profile image
      nocaffeine

      Informative, thank you, but god damn the poor quality of this video is making my head hurt.

    • User profile image
      rogreen

      @nocaffeine:Are you watching the video in the browser or did you download the WMV?

      Robert

    • User profile image
      HugoAgueroR

      @rogreen: Thanks Robert, all the methods work perfectly what I mean is that the name of the snippet don't appear automatically as I type.  If you look at the video at 11:57 as you type "propwithnotify"  you have intellisense showing the rest of the snippet name. I don't know why or I'm just a beginner but I thought that will help even more for a quick coding. Maybe I'm just doing the things wrong Smiley

    • User profile image
      rogreen

      @HugoAgueroR:Ahah! Now I see. Go to Tools|Options|Text Editor|C#. Make sure you check Show completion list after a character is typed. Click OK and see if it works now.

      Robert

    • User profile image
      HugoAgueroR

      @rogreen: OK perfect. Thanks

    • User profile image
      rogreen

      @HugoAgueroR:Glad we got this figured out! Smiley

    • User profile image
      salam

      thanks for the video. I didn't use it because it was confusing.

    • User profile image
      pnattrass

      Great toolbox video! Snippets has been one of those things I was always 'going to look at one day'. 

      Now I see the joy of those mysterious entries on the intellisense drop downs.

    Comments closed

    Comments have been closed since this content was published more than 30 days ago, but if you'd like to continue the conversation, please create a new thread in our Forums, or Contact Us and let us know.