Shortcut to create properties in Visual Studio?

后端 未结 16 1870
暖寄归人
暖寄归人 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:20

    What I liked in the IDE was that I was able to write a few variables like:

        private int id;
        private string name;
        private string version;
        private string description;
        private string status;
        private string symbol;
    

    Notice, that the variable names start with small letters, and then select the whole block, and press Ctrl+R, Ctrl+E, Apply. The properties are generated with the capital letter:

        public int Id
        {
            get
            {
                return id;
            }
    
            set
            {
                id = value;
            }
        }
    

    etc.

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

    If you are using Visual Studio 2013, 2015 or above, just click the link below. It will give you the full shortcuts in Visual Studio!

    Visual C# Code Snippets

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

    You could type "prop" and then press tab twice. That will generate the following.

    public TYPE Type { get; set; }
    

    Then you change "TYPE" and "Type":

    public string myString {get; set;}
    

    You can also get the full property typing "propfull" and then tab twice. That would generate the field and the full property.

    private int myVar;
    
    public int MyProperty
    {
        get { return myVar;}
        set { myVar = value;}
    }
    
    0 讨论(0)
  • 2020-12-02 04:31

    Using VsVim the code snippets seem to work a little funny. The shortcut I was looking for when I ended up here is much simpler: after a member name type {g;s;

    I have delimiter auto-closing turned on, so the closing brace appears on {, and typing a semicolon triggers an autocomplete for get and set.

    It works on VS2013 and VS2015, and VS2012 just lacks the automatic brace matching.

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