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:
![]()
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:
![]()
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; } }
Other post in Advanced CSharp
- Two Interfaces. Same Method. Two meanings. - September 29th, 2008
- Making values nullable - October 9th, 2008
- CSharp's Property Shortcuts - October 23rd, 2008
- Readonly variables in CSharp? Really?! - October 29th, 2008
- Dispose with Using - November 10th, 2008
- Delegates in .NET - December 4th, 2008
- Using Sealed in CSharp - December 8th, 2008
- CSharp checked and unchecked - December 11th, 2008
- Advanced CSharp - unsafe mode - December 15th, 2008
- Volatile variables and CSharp threads - December 22nd, 2008
- What is the global keyword in CSharp? - December 29th, 2008
- CSharp fixed keyword - January 5th, 2009
- using - There's more there than you are using - February 2nd, 2009
- Stackalloc in CSharp - February 16th, 2009
- Removing Warnings from CSharp Compile Cycle - March 10th, 2009
- && vs & and | vs ||... What's the difference? - March 16th, 2009
- Advanced CSharp - yield - March 25th, 2009
- Just say “No!” to C# Regions? Really?! - April 16th, 2009
- C# “” better than string.Empty? - April 20th, 2009
- .Net String Pool – Not Just For The Compiler - April 22nd, 2009
- CSharp ?? Operator - May 18th, 2009
- Using VB.NET From CSharp - July 1st, 2009
- Dispose, Finalize and SuppressFinalize - July 9th, 2009
- What is .NET’s Object.GetHashCode() Used For? - August 5th, 2009
- ASP.NET Substitution Control - October 22nd, 2009
- Transaction Tracking Typed Datasets Using SqlTransaction - July 20th, 2010