vs 2010代码片段

代码模板就是预先定义好的一个代码片段。VS中,我们只要输入模板的名称,然后连按两下TAB,VS就可以给我们把代码片段补充完整。

1. 内置代码片段

关键字 生成
for for(int i = 0; i < length; i++){}
foreach foreach (var item in collection) {}
do do… while loop
while while (true)  {}
if if (true)  { }
try try….catch…
class class MyClass{}
ctor 根据当前的类名生成空构造函数
cw Console.WriteLine()
exception 自定义异常类模板
indexer 索引器模板
mbox MessageBox.Show()
prop 自动属性 get;set;
propfull 传统属性(私有字段、封装get;set)
propg 自动属性 get; private set;

2. 修改代码片段

找到代码片段的位置

打开vs 2010

 

 例如修改class.snippet, 内容如下:

<?xml version="1.0" encoding="utf-8" ?>
<CodeSnippets  xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
    <CodeSnippet Format="1.0.0">
        <Header>
            <Title>class</Title>
            <Shortcut>class</Shortcut>
            <Description>Code snippet for class</Description>
            <Author>Microsoft Corporation</Author>
            <SnippetTypes>
                <SnippetType>Expansion</SnippetType>
                <SnippetType>SurroundsWith</SnippetType>
            </SnippetTypes>
        </Header>
        <Snippet>
            <Declarations>
                <Literal>
                    <ID>name</ID>
                    <ToolTip>Class name</ToolTip>
                    <Default>MyClass</Default>
                </Literal>
                <!-- 新增type和property变量, 20200514 -->
                <Literal>
                    <ID>type</ID>
                    <ToolTip>Property type</ToolTip>
                    <Default>String</Default>
                </Literal>
                <Literal>
                    <ID>property</ID>
                    <ToolTip>Property name</ToolTip>
                    <Default>MyProperty</Default>
                </Literal>
            </Declarations>
    <!-- 20200514 -->
    <!-- <Code Language="csharp"><![CDATA[class $name$ -->
    <!-- { -->
        <!-- $selected$$end$ -->
    <!-- }]]> -->
    
    <Code Language="csharp"><![CDATA[public class $name$
    {
        public $type$ $property$ { get; set; }$end$
    }]]>
    </Code>
        </Snippet>
    </CodeSnippet>
</CodeSnippets>
View Code

查看效果,输入class,连续按两次Tab键

3. 新增代码片段

例如新增Console.ReadLine片段,内容如下

<?xml version="1.0" encoding="utf-8" ?>
<CodeSnippets  xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
    <CodeSnippet Format="1.0.0">
        <Header>
            <Title>cr</Title>
            <Shortcut>cr</Shortcut>
            <Description>Code snippet for Console.ReadLine</Description>
            <Author>Microsoft Corporation</Author>
            <SnippetTypes>
                <SnippetType>Expansion</SnippetType>
            </SnippetTypes>
        </Header>
        <Snippet>
            <Declarations>
                <Literal Editable="false">
                    <ID>SystemConsole</ID>
                    <Function>SimpleTypeName(global::System.Console)</Function>
                </Literal>
            </Declarations>
            <Code Language="csharp"><![CDATA[$SystemConsole$.ReadLine($end$);]]>
            </Code>
        </Snippet>
    </CodeSnippet>
</CodeSnippets>
View Code

打开代码片段编辑器

 

 查看添加结果

查看效果,输入cr, 连续按两次Tab键

原文地址:https://www.cnblogs.com/LuckyZLi/p/12886698.html