Code snippets for methods in Visual Studio
Asked Answered
P

8

70

In Visual Studio I can type e.g.

for TAB TAB

and a code snippet pops in.

Are there built-in code snippets for private, public, etc. methods as well?

Positron answered 15/11, 2008 at 4:45 Comment(0)
B
123

ctor: Default constructor

prop: Property

propg: Read-only property

sim: static int main method

svm: static void main method

There's a good list here. And if you want to make your own, the Snippet Designer is very good.

Here are all the Visual C# code snippets for Visual Studio 2017

Bevin answered 15/11, 2008 at 4:57 Comment(6)
You insert it with a shortcut keyword, like meth<Tab><Tab>, by going into the Properties pane and setting the Shortcut option (e.g., "meth")?Opposite
The link to the list gives a 404Angrist
@FelixMM Odd it redirected for me. Anyway I've updated it to the new location.Bevin
@CameronMacFarland it's awesome. please update it with 2017 link :)Yaker
Is there no built in snippet that allows you to do something like sim or svm and allow you to tab through it and modify it?Eckhardt
They can be discovered by Ctrl + K, Ctrl + X (at least with some versions of Visual Studio, including Visual Studio 2012). Perhaps include it in the answer?Levant
D
43

You can download the method snippets as a Visual Studio Extension.

It supports the following:

method (typical method)

vmethod (virtual method)

smethod (static method)

xmethod (extension method)
  1. In Visual Studio, go to menu ToolsExtensions and Updates...

  2. Observe the Extensions and Updates window

  3. Enter "C# Methods Code Snippets" in the search field (upper right)

Duelist answered 23/4, 2015 at 13:22 Comment(3)
This is the only answer that actually answers the question - code snippets for methods (from the question: "...code snippets for private, public, etc. methods "). The other answers are just generic answers about snippets.Levant
I tried the search in Visual Studio 2012 (yes, I know) without finding the extension. I selected "Online" before entering "C# Methods Code Snippets". I looked at the first 20 pages of 25 hits each (no end in sight). But I did find a number interesting items: "Import Code Snippet", "CodeHelperTools", "NUnit VS Templates", "StyleCop", "Language Convert" (C# <-> VB.NET), "Script#" (transpile from C# to JavaScript),Levant
cont' - "Refactor" (.NET assembly browser and decompiler), "kiss!" (template-based code generator for C#, VB.NET, and SQL), "WPF MVVM project template", "T4Awesome", "SharpSetup" (WiX, create installers), "Units of Measurements", ".NET FTP library", and "Comment".Levant
B
17

If you want to see the list of all available snippets:

  • Press Ctrl + K and then Ctrl + X
Britneybritni answered 14/3, 2017 at 5:29 Comment(3)
Just to clarify, that's CTRL + K and then CTRL + X. For more info, see How to: Use Code Snippets (C#)Eckhardt
@Samir. Actually both works. However, updated to what Microsoft doc says.Britneybritni
That's right. It depends on how you formulate it. What was missing was "press and hold CTRL". But this works too.Eckhardt
C
13

Below are the steps I used to create a custom snippet for Visual Studio 2010, but the steps work for Visual Studio 2008.

Create a new text file named method.snippet and paste the following:

<?xml version="1.0" encoding="utf-8" ?>
<CodeSnippets  xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
    <CodeSnippet Format="1.0.0">
        <Header>
            <Title>method</Title>
            <Shortcut>method</Shortcut>
            <Description>Code snippet for method</Description>
            <Author>Kevin Hogg</Author>
            <SnippetTypes>
                <SnippetType>Expansion</SnippetType>
            </SnippetTypes>
        </Header>
        <Snippet>
            <Declarations>
                <Literal>
                    <ID>methodname</ID>
                    <ToolTip>Method name</ToolTip>
                    <Function>MethodName()</Function>
                    <Default>MethodNamePlaceholder</Default>
                </Literal>
            </Declarations>
            <Code Language="csharp"><![CDATA[public void $methodname$ ()
    {
        $end$
    }]]>
            </Code>
        </Snippet>
    </CodeSnippet>
</CodeSnippets>

Copy your file into the snippets folder in Windows Explorer:

  • Visual Studio 2010: C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC#\Snippets\1033\Visual C#
  • Visual Studio 2008: C:\Program Files (x86)\Microsoft Visual Studio 9.0\VC#\Snippets\1033\Visual C#

Once you save your file, the snippets are automatically loaded, so you can now open Visual Studio and type:

method<tab><tab>

*where <tab> is the Tab key on your keyboard.

You should now see the following created, with the MethodNamePlaceholder highlighted so you can change the name.

public void MethodNamePlaceholder()
{

}
Conclusive answered 8/10, 2013 at 12:32 Comment(4)
Please note I could only test this in Visual Studio 2010Conclusive
It also works on VS2013 Path : C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC#\Snippets\1033\Visual C#Anomalism
Works VS2015. Thanks. Great DYI. Check the box for learned something new today.Mullock
VS2019: C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\VC#\Snippets\1031\Visual C#Tortoni
S
9

Some of the snippets I use, also mentioned at MSDN, follows:

  1. '#if Creates a #if directive and a #endif directive.
  2. '#region Creates a #region directive and a #endregion directive.
  3. ~ Creates a destructor for the containing class.
  4. attribute Creates a declaration for a class that derives from Attribute.
  5. checked Creates a checked block.
  6. class Creates a class declaration.
  7. ctor Creates a constructor for the containing class.
  8. cw Creates a call to WriteLine.
  9. do Creates a do while loop.
  10. else Creates an else block.
  11. enum Creates an enum declaration.
  12. equals Creates a method declaration that overrides the Equals method defined in the Object class.
  13. exception Creates a declaration for a class that derives from an exception (Exception by default).
  14. for Creates a for loop.
  15. foreach Creates a foreach loop.
  16. forr Creates a for loop that decrements the loop variable after each iteration.
  17. if Creates an if block.
  18. indexer Creates an indexer declaration.
  19. interface Creates an interface declaration.
  20. invoke Creates a block that safely invokes an event.
  21. iterator Creates an iterator.
  22. iterindex Creates a "named" iterator and indexer pair by using a nested class.
  23. lock Creates a lock block.
  24. mbox Creates a call to MessageBox.Show. You may have to add a reference to System.Windows.Forms.dll.
  25. namespace Creates a namespace declaration.
  26. prop Creates an auto-implemented property declaration.
  27. propfull Creates a property declaration with get and set accessors.
  28. propg Creates a read-only auto-implemented property with a private "set" accessor.
  29. sim Creates a static int Main method declaration.
  30. struct Creates a struct declaration.
  31. svm Creates a static void Main method declaration.
  32. switch Creates a switch block.
  33. try Creates a try-catch block.
  34. tryf Creates a try-finally block.
  35. unchecked Creates an unchecked block.
  36. unsafe Creates an unsafe block.
  37. using Creates a using directive.
  38. while Creates a while loop.
Saville answered 22/9, 2016 at 4:48 Comment(1)
They can be discovered by Ctrl + K, Ctrl + X (at least with some versions of Visual Studio, including Visual Studio 2012). Perhaps include it in the answer?Levant
S
1

I made my own snippet for a method. The XML code for it is the following, and you can add it to a file called "my_method.snippet" (or whatever_you_want.snippet) in C:\Users\YOUR_USERNAME\Documents\Visual Studio 2012\Code Snippets\Visual C#\My Code Snippets (your path might be different because I use VS2012):

<CodeSnippet Format="1.0.0">
    <Header>
        <Title>method</Title>
        <Shortcut>method</Shortcut>
        <SnippetTypes>
            <SnippetType>Expansion</SnippetType>
        </SnippetTypes>
    </Header>
    <Snippet>
        <Declarations>
            <Literal>
                <ID>access_modifier</ID>
                <Default>private</Default>
            </Literal>
            <Literal>
                <ID>return_type</ID>
                <Default>void</Default>
            </Literal>
            <Literal>
                <ID>name</ID>
                <Default>New_method</Default>
            </Literal>
        </Declarations>
        <Code Language="csharp">
            <![CDATA[$access_modifier$ $return_type$ $name$ ()
    {
    $end$
    }]]>
        </Code>
    </Snippet>
</CodeSnippet>
Saidel answered 12/5, 2013 at 17:35 Comment(0)
H
0

You can create customs snippets. Like this:

http://www.mediafire.com/file/gz3tzjnydk5/meth.snippet

Heathenize answered 26/3, 2010 at 23:34 Comment(1)
That link is for a page about the meth.snippet XML document format (.SNIPP). That is far away from being about custom snippets. I suggest adding an example and/or more information to make this answer more useful.Levant
S
-2

The code snippet for properties is:

propTABTAB

Septivalent answered 15/11, 2008 at 4:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.