Code snippets for methods in Visual Studio

前端 未结 8 1500
青春惊慌失措
青春惊慌失措 2020-12-07 13:26

In Visual Studio I can type e.g.

for TAB TAB

and a code snippet pops in.

Are there built-in code

相关标签:
8条回答
  • 2020-12-07 13:43

    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.
    0 讨论(0)
  • 2020-12-07 13:45

    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()
    {
    
    }
    
    0 讨论(0)
  • 2020-12-07 13:50

    The code snippet for properties is:

    propTABTAB

    0 讨论(0)
  • 2020-12-07 13:51

    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)

    0 讨论(0)
  • 2020-12-07 13:52

    You can create customs snippets. Like this:

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

    0 讨论(0)
  • 2020-12-07 13:59

    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>
    
    0 讨论(0)
提交回复
热议问题