在VisualStudio2005和VisualStudio2008中,我们输入prop然后输入Tab键就可以智能插入一段代码,非常方便。如下图:
如果够懒的话,也可以自己创建自己经常写的代码段来提高写代码的效率,在VisualStudio2005和VisualStudio2008中,打开菜单Tools – Code Snippets Manager (Ctrl+K, Ctrl+B)可以查看Code Snippets的位置:
创建一个自己的Singleton代码段:(Singleton.snippet)











































其中的header部分简单描述了关于这个snippets的信息,如名称,快捷方式,作者,备注等。在<snippets>节点下,则逐个定义了在snippets中出现的属性,其中<default>指出了该属性默认的值。而在关键部分<code>,用<![CDATA]>的方式定义了snippets的基础架构,其中用$参数名的方式定义每个属性,十分简单。
(可以随便用编辑器,可以用微软的CodeSnippet编辑器,这里下载)
然后在VisualStudio2005和VisualStudio2008中,打开菜单Tools – Code Snippets Manager (Ctrl+K, Ctrl+B),点击导入,选择保存的Singleton.snippet文件位置即可。
以后在写代码时,输入singleton,然后按Tab键,即可插入一个Singleton模式的类。光标会自动停在类名上。(也可以点击鼠标右键,选择Insert Snippet…,然后选择自己的CodeSnippet即可。
同样可以创建自己定制的CodeSnippets,例如类模板,NUnit模板,读取Xml文件的代码段,保存Xml的代码段,抛出Exception的代码段,String.IsNullOrEmpty判断等等,来提高自己写代码的效率。
(其它Visual studio技巧:工欲善其事,必先利其器——图文并茂详解VisualStudio使用技巧一)
VS2010插件:Snippet Designer
有一个VisualStudio2010插件SnippetDesigner. 选中一段代码,然后右键“Export as Snippet”就可以很方便的管理和创建Snippet了。