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

 


Other post in Advanced CSharp

Other Related Items:

"Krylon Scp-901 ""Shortcuts"" Paint Pen - 1/3 Oz. Gold Leaf""Krylon Scp-901 ""Shortcuts"" Paint Pen - 1/3 Oz. Gold Leaf"Perfect size for small painting projects. Fast drying with a high gloss finish. Great for arts crafts & hobbies. Acid-free archival safe.

If you're new here, you may want to subscribe to the mailing list to get notifications of new post and a virtual tour of past topics. Thanks for visiting!

Related Post

Comments are closed.

DotNetNuke Sponsor

 

Most Valuable Blogger
Sponsor