可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
I have seen some people creating properties in C# really fast, but how did they do it?
What shortcuts are available in Visual Studio (currently using Visual Studio 2010) to create properties?
I am using C#.
For example,
public string myString {get;set;}
回答1:
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;}
}
回答2:
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.
回答3:
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.
回答4:
Type "propfull". It is much better to use, and it will generate the property and private variable.
Type "propfull" and then TAB twice.
回答5:
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.
回答6:
I think Alt+R+F is the correct one for creating property from a variable declaration
回答7:
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.
回答8:
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();
回答9:
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.
回答10:
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.
回答11:
ReSharper offers property generation in its extensive feature set. (It's not cheap though, unless you're working on an open-source project.)
回答12:
Type P + Tab + Tab.
Change the datatype, press TAB, change the property name, and press End + Enter.
回答13:
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
回答14:
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.
回答15:
In visual studio 2017 community, the key is ctrl + .
回答16:
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