Shortcut to create properties in Visual Studio?

后端 未结 16 1871
暖寄归人
暖寄归人 2020-12-02 03:42

I have seen some people creating properties in C# really fast, but how did they do it?

What shortcuts are available in Visual Studio (currently using Visual Stu

相关标签:
16条回答
  • 2020-12-02 04:16

    Type "propfull". It is much better to use, and it will generate the property and private variable.

    Type "propfull" and then TAB twice.

    0 讨论(0)
  • 2020-12-02 04:16

    Type P + Tab + Tab.

    Change the datatype, press TAB, change the property name, and press End + Enter.

    0 讨论(0)
  • 2020-12-02 04:16

    In C#:

    private string studentName;
    

    At the end of line after semicolon(;) Just Press

    Ctrl + R + E
    

    It will show a popup window like this: On click of Apply or pressing of ENTER it will generate the following code of property:

    public string StudentName
            {
                get
                {
                    return studentName;
                }
    
                set
                {
                    studentName = value;
                }
            }
    

    In VB:

    Private _studentName As String
    

    At the end of line (after String) Press, Make sure you place _(underscore) at the start because it will add number at the end of property:

    Ctrl + R + E
    

    The same window will appear:

    On click of Apply or pressing of ENTER it will generate the following code of property with number at the end like this:

    Public Property StudentName As String
            Get
                Return _studentName
            End Get
            Set(value As String)
                _studentName = value
            End Set
        End Property
    

    With number properties are like this:

    Private studentName As String
     Public Property StudentName1 As String
            Get
                Return studentName
            End Get
            Set(value As String)
                studentName = value
            End Set
        End Property
    
    0 讨论(0)
  • 2020-12-02 04:17

    Start from:

    private int myVar;
    

    When you select "myVar" and right click then select "Refactor" and select "Encapsulate Field".

    It will automatically create:

    {
        get { return myVar; }
        set { myVar = value; }
    }
    

    Or you can shortcut it by pressing Ctrl + R + E.

    0 讨论(0)
  • 2020-12-02 04:20

    Place cursor inside your field private int _i; and then Edit menu or RMB - Refactor - Encapsulate Field... (CtrlR, CtrlE) to create the standard property accessors.

    0 讨论(0)
  • 2020-12-02 04:20

    After typing "prop" + Tab + Tab as suggested by Amra, you can immediately type the property's type (which will replace the default int), type another tab and type the property name (which will replace the default MyProperty). Finish by pressing Enter.

    0 讨论(0)
提交回复
热议问题