visual-studio Visual Studio 中方法的代码片段

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/292164/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-22 09:56:00  来源:igfitidea点击:

Code snippets for methods in Visual Studio

visual-studiocode-snippets

提问by Edward Tanguay

In Visual Studio I can type e.g.

在 Visual Studio 中,我可以输入例如

for TABTAB

为了 TABTAB

and a code snippet pops in.

并弹出一个代码片段。

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

是否也有用于私有、公共等方法的内置代码片段?

回答by Cameron MacFarland

ctor: Default constructor

ctor: 默认构造函数

prop: Property

道具: 属性

propg: Read-only property

propg: 只读属性

sim: static int mainmethod

sim:static int main方法

svm: static void mainmethod

支持向量机static void main方法

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

有一个很好的列表在这里。如果您想自己制作,Snippet Designer非常好。

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

以下是 Visual Studio 2017 的所有 Visual C# 代码片段

回答by Scott Nimrod

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

您可以将方法片段下载为Visual Studio 扩展

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 Updateswindow

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

  1. 在 Visual Studio 中,转到菜单工具扩展和更新...

  2. 观察扩展和更新窗口

  3. 在搜索字段中输入“C# Methods Code Snippets”(右上角)

回答by Kevin Hogg

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

下面是我用于为 Visual Studio 2010 创建自定义代码段的步骤,但这些步骤适用于 Visual Studio 2008。

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

创建一个名为method.snippet的新文本文件并粘贴以下内容:

<?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:

将您的文件复制到 Windows 资源管理器中的代码段文件夹中:

  • 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#
  • 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:

保存文件后,代码段将自动加载,因此您现在可以打开 Visual Studio 并键入:

method<tab><tab>

*where <tab> is the Tabkey on your keyboard.

*其中 <tab> 是Tab键盘上的键。

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

您现在应该看到创建了以下内容,其中 MethodNamePlaceholder 突出显示,以便您可以更改名称。

public void MethodNamePlaceholder()
{

}

回答by ANewGuyInTown

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

如果您想查看所有可用的列表snippet

  • Press CTRL+Kand then CTRL+X
  • CTRL+K然后CTRL+X

回答by UJS

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

我使用的一些片段,也在 MSDN 上提到,如下:

  1. '#ifCreates a #if directive and a #endif directive.
  2. '#regionCreates a #region directive and a #endregion directive.
  3. ~Creates a destructor for the containing class.
  4. attributeCreates a declaration for a class that derives from Attribute.
  5. checkedCreates a checked block.
  6. classCreates a class declaration.
  7. ctorCreates a constructor for the containing class.
  8. cwCreates a call to WriteLine.
  9. doCreates a do while loop.
  10. elseCreates an else block.
  11. enumCreates an enum declaration.
  12. equalsCreates a method declaration that overrides the Equals method defined in the Object class.
  13. exceptionCreates a declaration for a class that derives from an exception (Exception by default).
  14. forCreates a for loop.
  15. foreachCreates a foreach loop.
  16. forrCreates a for loop that decrements the loop variable after each iteration.
  17. ifCreates an if block.
  18. indexerCreates an indexer declaration.
  19. interfaceCreates an interface declaration.
  20. invokeCreates a block that safely invokes an event.
  21. iteratorCreates an iterator.
  22. iterindexCreates a "named" iterator and indexer pair by using a nested class.
  23. lockCreates a lock block.
  24. mboxCreates a call to MessageBox.Show. You may have to add a reference to System.Windows.Forms.dll.
  25. namespaceCreates a namespace declaration.
  26. propCreates an auto-implemented property declaration.
  27. propfullCreates a property declaration with get and set accessors.
  28. propgCreates a read-only auto-implemented property with a private "set" accessor.
  29. simCreates a static int Main method declaration.
  30. structCreates a struct declaration.
  31. svmCreates a static void Main method declaration.
  32. switchCreates a switch block.
  33. tryCreates a try-catch block.
  34. tryfCreates a try-finally block.
  35. uncheckedCreates an unchecked block.
  36. unsafeCreates an unsafe block.
  37. usingCreates a using directive.
  38. whileCreates a while loop.
  1. ' #if创建一个 #if 指令和一个 #endif 指令。
  2. ' #region创建一个#region 指令和一个#endregion 指令。
  3. ~为包含的类创建一个析构函数。
  4. attribute为派生自 Attribute 的类创建声明。
  5. 已检查创建一个已检查的块。
  6. class创建一个类声明。
  7. ctor为包含的类创建一个构造函数。
  8. cw创建对 WriteLine 的调用。
  9. do创建一个 do while 循环。
  10. else创建一个 else 块。
  11. enum创建一个枚举声明。
  12. equals创建覆盖 Object 类中定义的 Equals 方法的方法声明。
  13. 异常为派生自异常的类创建声明(默认情况下为异常)。
  14. for创建一个 for 循环。
  15. foreach创建一个 foreach 循环。
  16. forr创建一个 for 循环,在每次迭代后递减循环变量。
  17. if创建一个 if 块。
  18. indexer创建索引器声明。
  19. interface创建一个接口声明。
  20. invoke创建一个安全调用事件的块。
  21. 迭代器创建一个迭代器。
  22. iterindex使用嵌套类创建“命名”迭代器和索引器对。
  23. lock创建一个锁块。
  24. mbox创建对 MessageBox.Show 的调用。您可能需要添加对 System.Windows.Forms.dll 的引用。
  25. namespace创建一个命名空间声明。
  26. prop创建一个自动实现的属性声明。
  27. propfull使用 get 和 set 访问器创建属性声明。
  28. propg创建一个带有私有“set”访问器的只读自动实现属性。
  29. sim创建静态 int Main 方法声明。
  30. struct创建一个结构声明。
  31. svm创建静态 void Main 方法声明。
  32. switch创建一个开关块。
  33. try创建一个 try-catch 块。
  34. tryf创建一个 try-finally 块。
  35. unchecked创建一个未经检查的块。
  36. unsafe创建一个不安全的块。
  37. using创建一个 using 指令。
  38. while创建一个 while 循环。

回答by daniel1426

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):

我为一个方法制作了自己的片段。它的 XML 代码如下,您可以将其添加到 C:\Users\YOUR_USERNAME\Documents\Visual Studio 2012\Code Snippets\Visual C#\My 中名为“my_method.snippet”(或whatever_you_want.snippet)的文件中代码片段(您的路径可能不同,因为我使用的是 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>

回答by EdgarT

You can create customs snippets. Like this:

您可以创建海关片段。像这样:

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

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

回答by Soraz

The code snippet for properties is:

属性的代码片段是:

propTABTAB

支柱TABTAB