Visual Studio Code snippet for methods?

2019-01-30 10:27发布

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?

8条回答
别忘想泡老子
2楼-- · 2019-01-30 10:59

You can create customs snippets. Like this:

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

查看更多
兄弟一词,经得起流年.
3楼-- · 2019-01-30 11:03

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.
查看更多
干净又极端
4楼-- · 2019-01-30 11:09

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 all the Visual C# code snippets for VS 2017

查看更多
小情绪 Triste *
5楼-- · 2019-01-30 11:19

Below are the steps I used to create a custom snippet for Visual Studio 2010, but the steps work for 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()
    {

    }
查看更多
劳资没心,怎么记你
6楼-- · 2019-01-30 11:20

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

  • Press CTRL+K and then X
查看更多
戒情不戒烟
7楼-- · 2019-01-30 11:22

You can download the method snippets as a VS Extension.

Supports the following:

method (typical method)

vmethod (virtual method)

smethod (static method)

xmethod (extension method)
  1. In Visual Studio, go to Tools | Extensions and Updates

  2. Observe Extensions and Updates window

  3. Enter "C# Methods Code Snippets"

查看更多
登录 后发表回答