Sunday, August 9, 2015

Converting existing code to use C#6 Expression Bodied Properties

Converting all types of Simple Properties

Search for

[^\S\r\n]+(?<accessor>\w+)(?<override> override | )(?<typename>\w+) (?<pname>.*?)\r?\n\s*\{\r?\n\s*get(\r?\n)*\s*\{\s*return (?<returnexpr>.*?);\s*\}\s*\}
and replace with
${accessor} ${override}${typename} ${pname} => ${returnexpr};

For example the following properties will be matched and converted:

public override string Description 
{
    get 
    {
        return "The quick brown fox jumped over the lazy dog.";
    }
}
public string Description 
{
    get 
    {
        return "The quick brown fox jumped over the lazy dog.";
    }
}
public string Description 
{
    get { return "The quick brown fox jumped over the lazy dog."; }
}
public bool HasValue 
{
    get { return this.field; }
}
public virtual IEnumerable<BudgetBucket> Buckets
{
    get { return this.lookupTable.Values.OrderBy(b => b.Code).ToList(); }
}

All the above are changed to the new syntax:
public override string Description => "The quick brown fox jumped over the lazy dog.";

No comments:

Post a Comment