c# - How to generate getters and setters in Visual Studio?


Translate

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.


Всички отговори
  • Translate

    Rather than using ctrl+k,x you can also just type prop and then hit tab twice


  • Translate

    Visual Studio also has a feature that will generate a Property from a private variable.

    If you right-click on a variable, in the context menu that pops up click on the "Refactor" item. Then choose encapsulate field. This will create a getter/setter property for a variable.

    I'm not too big a fan of this technique as it is a little bit awkward to use if you have to create a lot of getters/setters, and it puts the property directly below the private field, which bugs me because I usually have all of my private fields grouped together, and this Visual Studio feature breaks my class' formatting.


  • Translate

    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 right mouse button, Choose context menu Refactor \ Encapsulate Field... then press OK.

      enter image description here

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

      enter image description here

    • This is result:

      enter image description here



    You also place cursor for choosing property, use Menu Edit \ Refactor \ Encapsulate Field...

    and

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

    become to

    public int ProductID { get; set; }
    

  • Translate

    By generate, do you mean auto-generate? If that's not what you mean:

    Visual Studio 2008 has the easiest implementation for this:

    public PropertyType PropertyName { get; set; }
    

    In the background this creates an implied instance variable to which your property is stored and retrieved.

    However if you want to put in more logic in your Properties, you will have to have an instance variable for it:

    private PropertyType _property;
    
    public PropertyType PropertyName
    {
        get
        {
            //logic here 
            return _property;
        }
        set
        {
            //logic here
            _property = value;
        }
     }
    

    Previous versions of Visual Studio always used this longhand method as well.


  • Translate

    you can also use "propfull" and hit TAB twice, variable and property with get and set will be generate.


  • Translate

    If you are using Visual Studio 2005 and up you can create a setter/getter real fast using the insert snippet command. Right click on your code click on Insert Snippet (Ctrl+k,x) and then choose "prop" form the list. Hope this helps.


  • Translate

    If you're using ReSharper, go into the ReSharper menu --> Code --> Generate ... (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 :-)


  • Translate

    use the propfull keyword.
    It will generate property and variable


  • Translate

    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.


  • Translate

    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 the field or not.


  • Translate

    In addition to the 'prop' snippet and auto-properties, there is a refactor option to let you select an existing field and expose it via a property. Also, if you don't like the 'prop' implementation, you can create your own snippets. Additionally, a 3rd party refactoring tool like resharper will give you even more features and make it easier to create more advanced snippets. I'd recommend Resharper if you can afford it.

    http://msdn.microsoft.com/en-us/library/f7d3wz0k(VS.80).aspx http://www.jetbrains.com/


  • Translate

    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.


  • Translate

    enter image description herebehalf of the visual studio tool we can easily generate c# properties using online tool called. c# propery generator.


  • Translate

    First get Extension just press (ctrl+shift+X) and install getter setter .... After this just select your variable and right click go to Command palette... And type getter ... It will suggest generate get and set methods click on this...


  • Translate

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