I'm doing repeated work on a List<string>
to build an instance of MyClass
, but for simplicity's sake (there are a lot of regular expressions and IndexOf
operations involved), I have to currently Trim each line after every operation:
static MyClass Populate (\List<str> strList)
{
MyClass myClassInstance = new MyClass();
Operation1(ref strList, myClassInstance);
TrimAllLines(strList);
Operation2(ref strList, myClassInstance);
TrimAllLines(strList);
//...
return myClassInstance;
}
Is there a good way (preferably a drop-in replacement ) to make it so that every time I write to strList
, each string within is automatically trimmed?
Things I've toyed with:
- A wrapper of
string
that trims on implicit conversion. Would lose string Intellisense, andIEnumerable
s do not similarly convert implicitly. - Inheriting
List<string>
with indexerget { return base[index]; } set { base[index] = value.Trim(); }
. The indexer is not overridable.