[c#] 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 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;}

This question is related to c# visual-studio properties code-snippets shortcut

The answer is


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


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.


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

Type "propfull" and then TAB twice.


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.


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.


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


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.


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


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

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.


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.


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


Examples related to c#

How can I convert this one line of ActionScript to C#? Microsoft Advertising SDK doesn't deliverer ads How to use a global array in C#? How to correctly write async method? C# - insert values from file into two arrays Uploading into folder in FTP? Are these methods thread safe? dotnet ef not found in .NET Core 3 HTTP Error 500.30 - ANCM In-Process Start Failure Best way to "push" into C# array

Examples related to visual-studio

VS 2017 Git Local Commit DB.lock error on every commit How to remove an unpushed outgoing commit in Visual Studio? How to download Visual Studio Community Edition 2015 (not 2017) Cannot open include file: 'stdio.h' - Visual Studio Community 2017 - C++ Error How to fix the error "Windows SDK version 8.1" was not found? Visual Studio Code pylint: Unable to import 'protorpc' Open the terminal in visual studio? Is Visual Studio Community a 30 day trial? How can I run NUnit tests in Visual Studio 2017? Visual Studio 2017: Display method references

Examples related to properties

Property 'value' does not exist on type 'EventTarget' How to read data from java properties file using Spring Boot Kotlin - Property initialization using "by lazy" vs. "lateinit" react-router - pass props to handler component Specifying trust store information in spring boot application.properties Can I update a component's props in React.js? Property getters and setters Error in Swift class: Property not initialized at super.init call java.util.MissingResourceException: Can't find bundle for base name 'property_file name', locale en_US How to use BeanUtils.copyProperties?

Examples related to code-snippets

Code snippet or shortcut to create a constructor in Visual Studio Shortcut to create properties in Visual Studio? Choose File Dialog Get human readable version of file size? How to detect if JavaScript is disabled?

Examples related to shortcut

Collapse all methods in Visual Studio Code How do I create a shortcut via command-line in Windows? window.close() doesn't work - Scripts may close only the windows that were opened by it How to automatically generate getters and setters in Android Studio How to update gradle in android studio? Is there a short cut for going back to the beginning of a file by vi editor? How to create a shortcut using PowerShell How do I call a function twice or more times consecutively? Eclipse comment/uncomment shortcut? 2D array values C++