Same logic across multiple C# properties?

Viewed 61

I've joined on a team with a lot of legacy code, and I'm trying to go through and make things a little neater. I see a lot of model and data classes that have copied code that looks like this:

private string _Info = null;
public string Info
{
    get 
    {
        return _Info;
    }
    set
    {
        if (Info != value)
        {
            _Info = value;
            SomeFunctionThatShouldRunOnChange();
        }
    }
}

And this pattern is copied for MANY properties (500+ lines). Similar but slightly different patterns abound where code is essentially copied for each property.

What approaches would address this, if any?

My guesses are:

  • Have a small custom class that implements this property
  • Have a generic collection backing it that does the same

I was hoping there would be a clean way to adjust a property as a pattern and use it for multiple variables.

Thanks!

0 Answers
Related