Anders G. Nordby

Lead Consultant at Itera

Monthly Archives: March 2013

C# Language Feature I’d Like to See

Just found a hard-to-spot error in someone’s code, and guess this won’t be the last time where I see something like this, where the private _field variable was accesses by something other than its wrapper:

private SomeType _field;
public SomeType Field
{
    get { return DoSomething(_field); }
    set { _field = ProcessValue(value); }
}

What I’d really like, is something like this:

[AccessibleFrom(this.Field)]
private SomeType _field;
public SomeType Field
{
    get { return DoSomething(_field); }
    set { _field = ProcessValue(value); }
}

I know it’s possible to avoid this kind of error by wrapping the stuff in its own class – but do you really want to make a separate class for each property of every class? There should be some easier way to avoid it.

Advertisements