Shortcut to create properties in Visual Studio?
I have seen some people creating properties in C# really fast, but how did they do it?
What s开发者_如何学JAVAhortcuts are available in Visual Studio (currently using Visual Studio 2010) to create properties?
I am using C#.
For example,
public string myString {get;set;}
You could type "prop" and then press tab twice. That will generate the following.
public TYPE Type { get; set; }
Then you change "TYPE" and "Type":
public string myString {get; set;}
You can also get the full property typing "propfull" and then tab twice. That would generate the field and the full property.
private int myVar;
public int MyProperty
{
get { return myVar;}
set { myVar = value;}
}
In addition to Amra's answer, you can find other snippets by typing
Ctrl + K, Ctrl + X
Which is mapped to Edit.InsertSnippet in my Visual Studio and shows you the full list of snippets available.
Also remember that you can configure your own snippets by using the Snippets Manager, which is available in the Tools
menu, Code Snippets Manager...
.
Basically you create a file *.snippet
and use the Import button in the Code Snippets Manager to add it to Visual Studio.
For a full tutorial you can go to the docs; Walkthrough: Create a code snippet.
In Visual Studio Code snippets are handled slightly different than in Visual Studio. You can access all snippets by typing Ctrl + Shift + P and type in snippet
. Two options should be available, Insert Snippet
and Preferences: Configure User Snippets
.
The former inserts a snippet from your list of snippets (using the Language Mode which you can see in the status bar), and with the latter you can create your own snippets for any Language Mode.
If you know the shortname you can just type that and use Tab to expand the snippet. For inserting a C# property you have three snippets available, prop
, propfull
, and propg
, for different purposes.
Place cursor inside your field private int _i;
and then Edit menu or RMB - Refactor - Encapsulate Field... (CtrlR, CtrlE) to create the standard property accessors.
Type "propfull". It is much better to use, and it will generate the property and private variable.
Type "propfull" and then TAB twice.
After typing "prop" + Tab + Tab as suggested by Amra,
you can immediately type the property's type (which will replace the default int
), type another tab and type the property name (which will replace the default MyProperty). Finish by pressing Enter.
I think Alt+R+F is the correct one for creating property from a variable declaration
Start from:
private int myVar;
When you select "myVar" and right click then select "Refactor" and select "Encapsulate Field".
It will automatically create:
{
get { return myVar; }
set { myVar = value; }
}
Or you can shortcut it by pressing Ctrl + R + E.
What I liked in the IDE was that I was able to write a few variables like:
private int id;
private string name;
private string version;
private string description;
private string status;
private string symbol;
Notice, that the variable names start with small letters, and then select the whole block, and press Ctrl+R, Ctrl+E, Apply. The properties are generated with the capital letter:
public int Id
{
get
{
return id;
}
set
{
id = value;
}
}
etc.
Type P + Tab + Tab.
Change the datatype, press TAB, change the property name, and press End + Enter.
When you write in Visual Studio,
public ServiceTypesEnum Type { get; set; }
public string TypeString { get { return this.Type.ToString();}}
ReSharper will keep suggesting to convert it to:
public string TypeString => Type.ToString();
Go to
Tools >> Options >> Text Editor >> C# >> IntelliSense
Under the Snippets behaviour section:
Make sure "Always include snippets" is selected.
I hope it works for you too.
ReSharper offers property generation in its extensive feature set. (It's not cheap though, unless you're working on an open-source project.)
If you are using Visual Studio 2013, 2015 or above, just click the link below. It will give you the full shortcuts in Visual Studio!
Visual C# Code Snippets
Using VsVim the code snippets seem to work a little funny. The shortcut I was looking for when I ended up here is much simpler: after a member name type {g;s;
I have delimiter auto-closing turned on, so the closing brace appears on {, and typing a semicolon triggers an autocomplete for get and set.
It works on VS2013 and VS2015, and VS2012 just lacks the automatic brace matching.
In visual studio 2017 community, the key is ctrl + .
In C#:
private string studentName;
At the end of line after semicolon(;) Just Press
Ctrl + R + E
It will show a popup window like this:
On click of Apply or pressing of ENTER it will generate the following code of property:public string StudentName
{
get
{
return studentName;
}
set
{
studentName = value;
}
}
In VB:
Private _studentName As String
At the end of line (after String) Press, Make sure you place _(underscore) at the start because it will add number at the end of property:
Ctrl + R + E
The same window will appear:
On click of Apply or pressing of ENTER it will generate the following code of property with number at the end like this:
Public Property StudentName As String
Get
Return _studentName
End Get
Set(value As String)
_studentName = value
End Set
End Property
With number properties are like this:
Private studentName As String
Public Property StudentName1 As String
Get
Return studentName
End Get
Set(value As String)
studentName = value
End Set
End Property
精彩评论