Monday, March 31, 2008

C# "prop" shortcut in Visual Studio 2008

Anyone who builds classes in Visual Studio hopefully uses the "prop" shortcut to generate a property with its backing variable. Just type "prop" and hit tab twice and you get this:


private int myVar;

public int MyProperty
{
get { return myVar; }
set { myVar = value; }
}


Update the fields and away you go. This is a major time saver. I was disappointed to see Microsoft change its behavior in the 2008 upgrade. The new behavior is to generate an automatic property:


public int MyProperty { get; set; }


I learned recently where these shortcuts, or snippets are stored. It turns out it's pretty easy to modify or add to them. In Visual Studio 2005, you can find them here:

C:\Program Files\Microsoft Visual Studio 8\VC#\Snippets\1033\Visual C#

For 2008, here:

C:\Program Files\Microsoft Visual Studio 9.0\VC#\Snippets\1033\Visual C#

It appears that there aren't that many changes between versions. Of course, the one they changed is the one I suspect most of us use the most often. Shame on them.

I'm not here to argue the merits of full vs. automatic properties. I'm all for automatic properties. The problem happens when we return from VS2008 "Hello World" example to our real world code base. VS2008 happily upgrades an assembly I created in VS2005 but keeps it backward compatible. However, it seems that it is not smart enough to recognize that this assembly is targeting the 2.0 framework or rather that the project will still be used in VS2005. (I suppose one might argue "how would it know?") All those automatic properties won't compile in the 2.0 compiler while the 3.0+ compilers expand them out automatically.

So I decided that rather than cursing every time I have to manually expand a property I would instead fix the problem. Simply manipulating the .snippet files in the directory mentioned earlier does the trick.

I copied the "prop" snippet from the VS2005 directory into that for 2008 and renamed the "prop" snippet in 2008 "aprop" ("propa" was taken). You just need to be sure you edit the snippet XML to rename the snippet's shortcut and name as well, they are the values that show up in the IDE.

An interesting side note to this: Having forgetting to update the snippet shortcut and name in its XML, I tried it out and discovered that VS recognizes the duplicate names. It prompts you with "Multiple Snippets" and you must make another choice. Neat. Someone was thinking.

8 comments:

Anonymous said...

Pete, this is quite helpful. Thanks much.

Markus said...

Thank you for this great tip. I worried every day about that new prop!

Craig said...

I was thinking "what the???". Glad you confirmed this for me.

Thx!

Prawin said...

I was searching for this for days now and finally found it in ur post... thanks Pete.

Anonymous said...

Great! Thanks.

Lisa said...

For 2010, you'll type "propfull" then tab, tab! It's Done! Snipped and backing field!

tags: private myVar, public MyProperty, get set

gVista said...

try propfull it is just like the old prop, also try propg it makes a read only property gVista.com

gVista said...

try porpfull it is just like the old prop, also try propg it makes a read only property. gVista.com