298

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;}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131

17 Answers17

624

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;}
}
Amra
  • 24,780
  • 27
  • 82
  • 92
  • 7
    You have forgotten to name it "Code Snippet" :) – PVitt Oct 06 '10 at 10:10
  • You can also create your own snippets with the snippet editor. – Johann Blais Oct 06 '10 at 11:38
  • 15
    also, propg will create: public int MyProperty { get; private set; } – Amc_rtty Nov 26 '12 at 00:17
  • 4
    You can also edit the code snippet, in the folder VC#\Snippets\1033\Visual C#\" under your Visual Studio install. I edited my propfull.snippet to make the backing field `protected` instead of `private`, for example. – Paul Smith Feb 15 '13 at 21:46
  • 11
    If you just type P and TAB twice then it will also create int property. – Adnan Bhatti Feb 24 '13 at 04:27
  • Is there a way to easily add something like "virtual" to the prop shortcut? When I use it, I only have the slots for type and name. Yeah, I can manually go in, but I can't tab through with virtual. – ScubaSteve May 07 '15 at 17:41
  • 2
    You can then continue to hit Tab to select and overwrite the type/names. Not everyone seems to realise :-) – Marty Jun 27 '18 at 23:41
  • thanks i was wondering why intellisense was not working for days. didn't realize that the tab should be pressed **twice**. – Amir Dora. Jun 17 '20 at 10:01
87

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.

Patrick
  • 17,669
  • 6
  • 70
  • 85
25

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.

Mike
  • 2,995
  • 1
  • 18
  • 14
  • 1
    Not everybody is using the same keyboard layout (not to mention ReSharper et al). Might be good to add where you can find this same functionality in the menus. – sliderhouserules Dec 02 '14 at 18:17
13

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

Type "propfull" and then TAB twice.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Hitesh
  • 131
  • 1
  • 2
12

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
BillDarcy
  • 320
  • 4
  • 8
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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
6

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

Marko
  • 20,385
  • 13
  • 48
  • 64
4

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.

Developer Guy
  • 2,318
  • 6
  • 19
  • 37
Jaro64
  • 151
  • 1
  • 7
  • This does not really answer the question, does it? – mrun Apr 24 '18 at 13:51
  • This was a kind of shorcut I was looking for, but you are right. My answer focuses on how to prepare the variables to have properties ready with no edit after applying the shortcut. – Jaro64 Apr 24 '18 at 17:16
  • This was a kind of shortcut I was looking for, and ended up on this page. The original question is how to create properties in C# really fast. In many use cases under this surely, this will be where you have a private field and want avoid making this a public field (this is my case). If I have my private field(s) written, and want full public properties down asap I think this is answering question perfectly in this large subset of cases. Simples. – Will Croxford Jul 16 '18 at 14:06
  • This is great but mine gives me this 'get =>' shorthand which I don't like. Is there a setting for what gets generated? – Paul McCarthy Mar 16 '21 at 23:07
3

Type P + Tab + Tab.

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

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Shuvankar Sarkar
  • 409
  • 4
  • 11
3

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();
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Mahdi Alkhatib
  • 1,954
  • 1
  • 29
  • 43
3

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.

Gabriel G
  • 680
  • 8
  • 7
1

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

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Lester
  • 1,112
  • 2
  • 15
  • 22
1

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

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Paul Ruane
  • 37,459
  • 12
  • 63
  • 82
0

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.

kitsu.eb
  • 2,996
  • 1
  • 26
  • 28
0

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

stories2
  • 466
  • 1
  • 5
  • 20
0

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
Muhammad Awais
  • 4,238
  • 1
  • 42
  • 37
0

You can define a field by pressing: Ctrl+. and insert its property by selecting Encapsulate field: [..].

enter image description here

Andreas Violaris
  • 2,465
  • 5
  • 13
  • 26
Hejabi
  • 1
  • 2