The Darkside

Shedding light on things and stuff

 
  Home :: Contact :: Syndication  :: Login
  75 Posts :: 0 Stories :: 49 Comments :: 2 Trackbacks

Ads

Archives

Post Categories

Open Source Projects

Other Blogs

This is a really quick method to do a find and replace in VS2008 to replace your get/set properties to the new syntax, using regular expressions. You can even use this for C#2.0 targetted projects, the main requirement is the VS2008 (or msbuild.exe from framework 3.5).

If you have a few of these style property declarations:

private int _foo;
public int Foo
{
    get
    {
        return _foo;
    }
    set
    {
        _foo = value;
    }
}

and you want to change them to

public int Foo { get; set; }

you can make use of the VS Find and Replace dialog. In the "Find Options" group, check the "Use" box, and select "Regular Expressions".

The search pattern is:

UPDATED 2008/01/23: Minor mistake with the last get/set in a class

\n[:b\n]+\{[:b\n]+get[:b\n\{:a;_]+return[:b\n:a;\}_$()]+set[:b\n\{:a;=_$()]+value[);]+([:b\n]+\})^2

and the replace text is:

{ get; set; }

The regular expression will skip any get/set properties that have extra code (except brackets/spacing). Once these have been replace, you can remove the unused private variables, or use ReSharper to remove them for you :)

posted on Thursday, January 17, 2008 11:35 AM

Feedback

# re: Change to new Get/Set with RegEx 1/17/2008 1:05 PM Grant Merwitz
What do you mean by "use this for C#2.0 targetted projects"?

Does this mean this syntax will work on an application running in 2.0?
Or will 3.5 need to be installed for this to work?

# re: Change to new Get/Set with RegEx 1/17/2008 1:29 PM Darksider
Framework 3.5 won't be required if you set the target framework to 2.0. This is a VS2008/MSBuild3.5 feature only, though, so if you alter source files and try and use them in VS2005, it'll throw a compile error.

Comments have been closed on this topic.