Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Easy way to generate Getters and Setters in Visual Studio

Is there a way to generate getters and setters in Visual Studio? I'm trying with Alt + R, F and i get this:

public String Denomination
{
    get { return denomination; }
    set { denomination = value; }
}

and what I want is this:

public String getDenomination()
{
    return Denomination;
}

public void setDenomination(String Denomination)
{
    this.Denomination = Denomination;
}

is there a way to do that?

like image 911
Gundwane Avatar asked Dec 01 '22 15:12

Gundwane


2 Answers

You can use the prop code snippet to create automatic properties.

Type prop, and press Tab. You can then change the Type and name of the property.

In your simple case, where no additional logic is required, there's no needed for backing fields.

like image 82
user3907267 Avatar answered Dec 18 '22 18:12

user3907267


I do not think there is a built in way to do it out of the box with Visual Studio, but they do provide a way for you to add that feature.

What you will need to do is create a Code Snippet that creates those two methods and add the snippet to your %USERPROFILE%\Documents\Visual Studio 2013\Code Snippets\Visual C#\My Code Snippets folder. Once you do that you will be able to type the snippet name in and hit tab and it will fill in the text you are looking for.

like image 36
Scott Chamberlain Avatar answered Dec 18 '22 18:12

Scott Chamberlain