Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# shortcut or shorthand getter setter

Is there a short way to create the getter and setter in c#?

public string fname {get; set;}  

Is there short hand to generate {get; set;}?

like image 932
Yogurt The Wise Avatar asked Nov 04 '11 20:11

Yogurt The Wise


2 Answers

yea type prop and press TAB. Visual Studio has a snippet for automatic property.

For property with public get and private set, you can use propg and press TAB.

For complete non auto property you can use propfull and press TAB.

like image 172
Muhammad Hasan Khan Avatar answered Oct 02 '22 13:10

Muhammad Hasan Khan


If you just want the getter and setter, as orignally asked for, you could also create a custom snippet:

<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">     <CodeSnippet Format="1.0.0">         <Header>             <Title>GetSet</Title>             <Description>Inserts getter/setter shorthand code</Description>             <Shortcut>gs</Shortcut>         </Header>         <Snippet>             <Code Language="CSharp">                 <![CDATA[{ get; set; }$end$]]>             </Code>         </Snippet>     </CodeSnippet> </CodeSnippets> 

Save the above as a .snippet in your snippets folder. Typing 'gs' and pressing Tab will insert { get; set;} and move to the end of the line.


Edit

In VS Code, this would be a custom user snippet in your csharp.json file:

"Getter Setter": {     "prefix": "gs",     "body": [         "\\{ get; set; \\}",         "$1"     ],     "description": "Insert shorthand autoproperties" } 

Either of these examples could easily be modified/duplicated to also do { get; } (use a readonly backing field) or { get; private set; }

like image 39
brichins Avatar answered Oct 02 '22 11:10

brichins