C#常用代码片段备忘
常用的代码片段:
ctor 自动生成默认的构造函数
prop自动生成get set方法
cw 自动生成Console.WriteLine()
输入override 然后按空格,自动显示父类中所有可重写的成员列表
Exception 自动生成一个新的遵循.NET最佳时间的异常类
以下是从visual studio中整理出来的常用代码片段,以作备忘
快捷键: eh
用途: 类中事件实现函数模板
private void MyMethod(object sender, EventArgs e)
{
throw new NotImplementedException();
}
快捷键: xmethod 有4个
用途: 类中公有静态方法的函数模板
public static void MyMethod(this object value)
{
throw new NotImplementedException();
}
快捷键: method 有4个
用途: 类中公有函数的模板
public void MyMethod()
{
throw new NotImplementedException();
}
快捷键: seh
用途: 类中私有静态方法的函数模板
private static void MyMethod(object sender, EventArgs e)
{
throw new NotImplementedException();
}
快捷键: smethod 有4个
public static void MyMethod()
{
throw new NotImplementedException();
}
快捷键: vmethod 有4个
用途: 类中虚函数的模板
public virtual void MyMethod()
{
throw new NotImplementedException();
}
快捷键: propdp
用途: 定义依赖属性的模板
public int MyProperty
{
get { return (int)GetValue(MyPropertyProperty); }
set { SetValue(MyPropertyProperty, value); }
}
// Using a DependencyProperty as the backing store for MyProperty. This enables animation, styling, binding, etc...
public static readonly DependencyProperty MyPropertyProperty =
DependencyProperty.Register("MyProperty", typeof(int), typeof(ownerclass), new PropertyMetadata(0));
快捷键: propa
用途: 定义附加属性的模板
public static int GetMyProperty(DependencyObject obj)
{
return (int)obj.GetValue(MyPropertyProperty);
}
public static void SetMyProperty(DependencyObject obj, int value)
{
obj.SetValue(MyPropertyProperty, value);
}
// Using a DependencyProperty as the backing store for MyProperty. This enables animation, styling, binding, etc...
public static readonly DependencyProperty MyPropertyProperty =
DependencyProperty.RegisterAttached("MyProperty", typeof(int), typeof(ownerclass), new PropertyMetadata(0));
快捷键: wde
用途: Windows工作流模式下创建依赖属性事件的模板
public static System.Workflow.ComponentModel.DependencyProperty InvokeEvent = System.Workflow.ComponentModel.DependencyProperty.Register("Invoke", typeof(EventHandler), typeof(Obj));
[System.ComponentModel.Description("Invoke")]
[System.ComponentModel.Category("Invoke Category")]
[System.ComponentModel.Browsable(true)]
[System.ComponentModel.DesignerSerializationVisibilityAttribute(System.ComponentModel.DesignerSerializationVisibility.Visible)]
public event EventHandler Invoke
{
add
{
base.AddHandler(Obj.InvokeEvent, value);
}
remove
{
base.RemoveHandler(Obj.InvokeEvent, value);
}
}
快捷键: wdp
public static System.Workflow.ComponentModel.DependencyProperty MyPropertyProperty = System.Workflow.ComponentModel.DependencyProperty.Register("MyProperty", typeof(string), typeof(Obj));
[System.ComponentModel.Description("MyProperty")]
[System.ComponentModel.Category("MyProperty Category")]
[System.ComponentModel.Browsable(true)]
[System.ComponentModel.DesignerSerializationVisibilityAttribute(System.ComponentModel.DesignerSerializationVisibility.Visible)]
public string MyProperty
{
get
{
return ((string)(base.GetValue(Obj.MyPropertyProperty)));
}
set
{
base.SetValue(Obj.MyPropertyProperty, value);
}
}
快捷键: testc
用途: 新建一个C#的测试单元类的模板
[Microsoft.VisualStudio.TestTools.UnitTesting.TestClass]
public class MyTestClass
{
}
快捷键: testm
用途: 在C#的测试单元类中新增一个测试方法
[TestMethod]
public void MyTestMethod()
{
}