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
Code snippets for methods in Visual Studio
提问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
回答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)
In Visual Studio, go to menu Tools→ Extensions and Updates...
Observe the Extensions and Updateswindow
Enter "C# Methods Code Snippets" in the search field (upper right)
在 Visual Studio 中,转到菜单工具→扩展和更新...
观察扩展和更新窗口
在搜索字段中输入“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 上提到,如下:
- '#ifCreates a #if directive and a #endif directive.
- '#regionCreates a #region directive and a #endregion directive.
- ~Creates a destructor for the containing class.
- attributeCreates a declaration for a class that derives from Attribute.
- checkedCreates a checked block.
- classCreates a class declaration.
- ctorCreates a constructor for the containing class.
- cwCreates a call to WriteLine.
- doCreates a do while loop.
- elseCreates an else block.
- enumCreates an enum declaration.
- equalsCreates a method declaration that overrides the Equals method defined in the Object class.
- exceptionCreates a declaration for a class that derives from an exception (Exception by default).
- forCreates a for loop.
- foreachCreates a foreach loop.
- forrCreates a for loop that decrements the loop variable after each iteration.
- ifCreates an if block.
- indexerCreates an indexer declaration.
- interfaceCreates an interface declaration.
- invokeCreates a block that safely invokes an event.
- iteratorCreates an iterator.
- iterindexCreates a "named" iterator and indexer pair by using a nested class.
- lockCreates a lock block.
- mboxCreates a call to MessageBox.Show. You may have to add a reference to System.Windows.Forms.dll.
- namespaceCreates a namespace declaration.
- propCreates an auto-implemented property declaration.
- propfullCreates a property declaration with get and set accessors.
- propgCreates a read-only auto-implemented property with a private "set" accessor.
- simCreates a static int Main method declaration.
- structCreates a struct declaration.
- svmCreates a static void Main method declaration.
- switchCreates a switch block.
- tryCreates a try-catch block.
- tryfCreates a try-finally block.
- uncheckedCreates an unchecked block.
- unsafeCreates an unsafe block.
- usingCreates a using directive.
- whileCreates a while loop.
- ' #if创建一个 #if 指令和一个 #endif 指令。
- ' #region创建一个#region 指令和一个#endregion 指令。
- ~为包含的类创建一个析构函数。
- attribute为派生自 Attribute 的类创建声明。
- 已检查创建一个已检查的块。
- class创建一个类声明。
- ctor为包含的类创建一个构造函数。
- cw创建对 WriteLine 的调用。
- do创建一个 do while 循环。
- else创建一个 else 块。
- enum创建一个枚举声明。
- equals创建覆盖 Object 类中定义的 Equals 方法的方法声明。
- 异常为派生自异常的类创建声明(默认情况下为异常)。
- for创建一个 for 循环。
- foreach创建一个 foreach 循环。
- forr创建一个 for 循环,在每次迭代后递减循环变量。
- if创建一个 if 块。
- indexer创建索引器声明。
- interface创建一个接口声明。
- invoke创建一个安全调用事件的块。
- 迭代器创建一个迭代器。
- iterindex使用嵌套类创建“命名”迭代器和索引器对。
- lock创建一个锁块。
- mbox创建对 MessageBox.Show 的调用。您可能需要添加对 System.Windows.Forms.dll 的引用。
- namespace创建一个命名空间声明。
- prop创建一个自动实现的属性声明。
- propfull使用 get 和 set 访问器创建属性声明。
- propg创建一个带有私有“set”访问器的只读自动实现属性。
- sim创建静态 int Main 方法声明。
- struct创建一个结构声明。
- svm创建静态 void Main 方法声明。
- switch创建一个开关块。
- try创建一个 try-catch 块。
- tryf创建一个 try-finally 块。
- unchecked创建一个未经检查的块。
- unsafe创建一个不安全的块。
- using创建一个 using 指令。
- 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:
您可以创建海关片段。像这样:
回答by Soraz
The code snippet for properties is:
属性的代码片段是:
propTABTAB
支柱TABTAB

