天天看點

C#常用代碼片段備忘

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()
        {

        }