How can we generate getters and setters in Visual Studio?

后端 未结 16 2116
盖世英雄少女心
盖世英雄少女心 2020-12-07 07:46

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

But any more explicit explication or comment on good pr

相关标签:
16条回答
  • 2020-12-07 07:54

    I use Visual Studio 2013 Professional.

    • Place your cursor at the line of an instance variable.

      Enter image description here

    • Press combine keys Ctrl + R, Ctrl + E, or click the right mouse button. Choose context menu RefactorEncapsulate Field..., and then press OK.

      Enter image description here

    • In Preview Reference Changes - Encapsulate Field dialog, press button Apply.

      Enter image description here

    • This is result:

      Enter image description here



    You also place the cursor for choosing a property. Use menu EditRefactorEncapsulate Field...

    • Other information:

      Since C# 3.0 (November 19th 2007), we can use auto-implemented properties (this is merely syntactic sugar).

      And

      private int productID;
      
      public int ProductID
      {
          get { return productID; }
          set { productID = value; }
      }
      

      becomes

      public int ProductID { get; set; }
      
    0 讨论(0)
  • 2020-12-07 07:55

    Rather than using Ctrl + K, X you can also just type prop and then hit Tab twice.

    0 讨论(0)
  • 2020-12-07 07:55

    If you're using ReSharper, go into the ReSharper menu → CodeGenerate...

    (Or hit Alt + Ins inside the surrounding class), and you'll get all the options for generating getters and/or setters you can think of :-)

    0 讨论(0)
  • 2020-12-07 07:57

    I created my own snippet that only adds {get; set;}. I made it just because I find propTab 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>
    </CodeSnippets>
    

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

    0 讨论(0)
  • 2020-12-07 07:58

    You just simply press Alt + Ins in Android Studio.

    After declaring variables, you will get the getters and setters in the generated code.

    0 讨论(0)
  • 2020-12-07 07:59

    In Visual Studio Community Edition 2015 you can select all the fields you want and then press Ctrl + . to automatically generate the properties.

    You have to choose if you want to use the property instead of the field or not.

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