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条回答
我只想做你的唯一
2楼-- · 2019-01-16 01:00

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.

查看更多
霸刀☆藐视天下
3楼-- · 2019-01-16 01:00

use the propfull keyword.
It will generate property and variable

查看更多
孤傲高冷的网名
4楼-- · 2019-01-16 01:02

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/

查看更多
Fickle 薄情
5楼-- · 2019-01-16 01:03

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.

查看更多
在下西门庆
6楼-- · 2019-01-16 01:07

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.

查看更多
成全新的幸福
7楼-- · 2019-01-16 01:09

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; }
查看更多
登录 后发表回答