How to generate getters and setters in Visual Stud

2019-01-16 00:27发布

By "generate", I mean auto-generation of the code necessary for a particuliar selected (set of) variable(s).

But any more explicit explication or comment on good practice is welcome.

15条回答
一纸荒年 Trace。
2楼-- · 2019-01-16 01:21

I know this is older than the sun, but figured I would post this as my answer because it just like doing it this way.

What I did was create my own snippet that ONLY adds {get; set;}. I made it just because I find prop > tab to be clunky.

<?xml version="1.0" encoding="utf-8"?>
<CodeSnippets
xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
  <CodeSnippet Format="1.0.0">
    <Header>
        <Title>get set</Title>
         <Shortcut>get</Shortcut>
    </Header>
    <Snippet>
        <Code Language="CSharp">
            <![CDATA[{get; set;}]]>
        </Code>
    </Snippet>
  </CodeSnippet>

With this, you type your PropType and PropName manually, then type get > tab and it will add the get set. Its nothing magical, but since I tend to type my access modifier first anyway, I may as well finish out the name and type.

查看更多
爷的心禁止访问
3楼-- · 2019-01-16 01:24

I don't have Visual Studio installed on my machine anymore (and I'm using Linux), but I do remember that there was an wizard hidden somewhere inside one of the menus that gave access to a class builder.

With this wizard, you could define all your classes' details, including methods and attributes. If I remember well, there was an option through which you could ask VS to create the setters and getters automatically for you.

I know it's quite vague, but check it out and you might find it.

查看更多
唯我独甜
4楼-- · 2019-01-16 01:25

You just simple press Alt+Ins in android studio after declaring variables, you will get the getters and setters in generating code.

查看更多
登录 后发表回答