Shortcut to create properties in Visual Studio?

C#Visual StudioPropertiesCode SnippetsShortcut

C# Problem Overview


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;}

C# Solutions


Solution 1 - C#

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;}
}

Solution 2 - C#

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.

Solution 3 - C#

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.

Solution 4 - C#

Type "propfull". It is much better to use, and it will generate the property and private variable.

Type "propfull" and then TAB twice.

Solution 5 - C#

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.

Solution 6 - C#

I think Alt+R+F is the correct one for creating property from a variable declaration

Solution 7 - C#

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.

Solution 8 - C#

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.

Solution 9 - C#

Type P + Tab + Tab.

Change the datatype, press TAB, change the property name, and press End + Enter.

Solution 10 - C#

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();

Solution 11 - C#

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.

Solution 12 - C#

ReSharper offers property generation in its extensive feature set. (It's not cheap though, unless you're working on an open-source project.)

Solution 13 - C#

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

Solution 14 - C#

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.

Solution 15 - C#

In visual studio 2017 community, the key is ctrl + .

Solution 16 - C#

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: enter image description here 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: enter image description here

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser467058View Question on Stackoverflow
Solution 1 - C#AmraView Answer on Stackoverflow
Solution 2 - C#PatrickView Answer on Stackoverflow
Solution 3 - C#MikeView Answer on Stackoverflow
Solution 4 - C#HiteshView Answer on Stackoverflow
Solution 5 - C#BillDarcyView Answer on Stackoverflow
Solution 6 - C#Bins Jose AzhakathuView Answer on Stackoverflow
Solution 7 - C#Emrah EsmeroğluView Answer on Stackoverflow
Solution 8 - C#Jaro64View Answer on Stackoverflow
Solution 9 - C#Shuvankar SarkarView Answer on Stackoverflow
Solution 10 - C#Mahdi AlkhatibView Answer on Stackoverflow
Solution 11 - C#Gabriel GView Answer on Stackoverflow
Solution 12 - C#Paul RuaneView Answer on Stackoverflow
Solution 13 - C#LesterView Answer on Stackoverflow
Solution 14 - C#kitsu.ebView Answer on Stackoverflow
Solution 15 - C#stories2View Answer on Stackoverflow
Solution 16 - C#Muhammad AwaisView Answer on Stackoverflow