C# shortcut or shorthand getter setter

后端 未结 4 1958
执念已碎
执念已碎 2020-12-29 01:31

Is there a short way to create the getter and setter in c#?

public string fname {get; set;} 

Is there short hand to generate {get; se

4条回答
  •  伪装坚强ぢ
    2020-12-29 02:27

    If you just want the getter and setter, as orignally asked for, you could also create a custom snippet:

    
        
            
    GetSet Inserts getter/setter shorthand code gs

    Save the above as a .snippet in your snippets folder. Typing 'gs' and pressing Tab will insert { get; set;} and move to the end of the line.


    Edit

    In VS Code, this would be a custom user snippet in your csharp.json file:

    "Getter Setter": {
        "prefix": "gs",
        "body": [
            "\\{ get; set; \\}",
            "$1"
        ],
        "description": "Insert shorthand autoproperties"
    }
    

    Either of these examples could easily be modified/duplicated to also do { get; } (use a readonly backing field) or { get; private set; }

提交回复
热议问题