问题
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?
回答1:
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.
回答2:
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.
回答3:
Its merely an opinion but I am not a big fan of properties since I began with java development and then switched to C#.
I know developers who love them and I know some who hate them but if your team for example wants to use getter and setter instead of properties then this snippet might be interesting to you.
I changed the one which generates properties to fit my needs but maybe its also right for you
<?xml version="1.0" encoding="utf-8" ?>
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
<CodeSnippet Format="1.0.0">
<Header>
<Title>getset</Title>
<Shortcut>getset</Shortcut>
<Description>Code snippet for Getter and Setter</Description>
<Author>bongo</Author>
<SnippetTypes>
<SnippetType>Expansion</SnippetType>
</SnippetTypes>
</Header>
<Snippet>
<Declarations>
<Literal>
<ID>type</ID>
<ToolTip>object type</ToolTip>
<Default>int</Default>
</Literal>
<Literal>
<ID>GSName</ID>
<ToolTip>Getter Setter name</ToolTip>
<Default>MyMethod</Default>
</Literal>
<Literal>
<ID>field</ID>
<ToolTip>The variable backing this Getter Setter</ToolTip>
<Default>myVar</Default>
</Literal>
</Declarations>
<Code Language="csharp"><![CDATA[private $type$ $field$;
/// <summary>
///
/// </summary>
/// <param name="value"></param>
public void Set$GSName$($type$ value)
{
$field$ = value;
}
/// <summary>
///
/// </summary>
/// <returns></returns>
public $type$ Get$GSName$()
{
return $field$;
}
$end$]]>
</Code>
</Snippet>
</CodeSnippet>
</CodeSnippets>
if you add it like Scott Chamerlain said or use the Code snippet manager of Visual studio in the Tools tab you can type getset and then press tab in Visual studio and it will generate this:
private int myVar;
/// <summary>
///
/// </summary>
/// <param name="value"></param>
public void SetMyMethod(int value)
{
myVar = value;
}
/// <summary>
///
/// </summary>
/// <returns></returns>
public int GetMyMethod()
{
return myVar;
}
来源:https://stackoverflow.com/questions/25648350/easy-way-to-generate-getters-and-setters-in-visual-studio