With

Ideally I’d write code like

with someObject do
{
Property1 = ...
Property2 = ...
}

However, that would mean modifying the language.

I could settle for a code sequence like:

With(someObject).Do(
i =>
{
i.Property1 = ...
i.Property2 = ...
}
);

Unfortunately, this doesn’t work either. What does work, though, is something like

someObject.Apply( i =>
{
i.Property1 = ...
i.Property2 = ...
}
);

where Apply is an extension method


static class ObjectExtensions

{

public static T Apply<T>( this T target, Action<T> action )

{

action( target );

return target;

}

}

 

Unless

I’d like to write code like


i.SomeMethod() unless( condition );

instead of

if( condition )
{
i.SomeMethod();
}

I need to do some research on this one.

Advertisements