CSharp’s Property Shortcuts

There are a lot of nice shortcuts in the csharp language that most of us never use.  But if you take the time to learn them, you can be as productive as a student I had who had learned all the keyboard shortcuts to windows so that he never had to take his hands off the keyboard.

It was just a bit frustrating as an instructor because every time he sat down to do an exercise he’d be asking, “what’s the keyboard shortcut for this step?”  I have to admit, though, he was definitely faster than anyone else who had ever taken the class.

prop and propg are two such shortcuts.

If you are anything like me, I bet you write all of your properties something like this:

private int m_MyProperty;
public int MyProperty
{
    get { return m_MyProperty; }
    set { m_MyProperty = value; }
}

typing out every character.  Maybe you use a bit of copy and paste, but did you know that all you have to do is type:

prop

and you’ll get this:

image

Since my code insertion plugin won’t show you the full impact, I have to show you an image.

Basically it stubs out the property so all you have to do is supply the missing details.

propg gives you this:

image

The only difference is that it makes the getter private.

My preferred method is to create a member variable:

private int m_MyProperty;

Right click the variable and select “Refactor” > “Encapsulate Field…” from the context menu.  Once I’m done, I’m left with the code I typed above.

private int m_MyProperty;
public int MyProperty
{
    get { return m_MyProperty; }
    set { m_MyProperty = value; }
}

Related Post

Leave a Reply

Comment Policy:

  • You must verify your comment by responding to the automated email that is sent to your email address. Unverified comments will never show.Leave a good comment that adds to the conversation and I'll leave your link in.
  • Leave me pure spam and I'll delete it.
  • Leave a general comment and I'll remove the link but keep the comment.

Notify me of followup comments via e-mail

Bear